Class BigIntegerMath
- java.lang.Object
-
- com.google.common.math.BigIntegerMath
-
public final class BigIntegerMath extends java.lang.ObjectA class for arithmetic on values of typeBigInteger.The implementations of many methods in this class are based on material from Henry S. Warren, Jr.'s Hacker's Delight, (Addison Wesley, 2002).
Similar functionality for
intand forlongcan be found inIntMathandLongMathrespectively.- Since:
- 11.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classBigIntegerMath.BigIntegerToDoubleRounder
-
Field Summary
Fields Modifier and Type Field Description private static doubleLN_10private static doubleLN_2(package private) static intSQRT2_PRECOMPUTE_THRESHOLD(package private) static java.math.BigIntegerSQRT2_PRECOMPUTED_BITS
-
Constructor Summary
Constructors Modifier Constructor Description privateBigIntegerMath()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.math.BigIntegerbinomial(int n, int k)Returnsnchoosek, also known as the binomial coefficient ofnandk, that is,n! / (k! (n - k)!).static java.math.BigIntegerceilingPowerOfTwo(java.math.BigInteger x)Returns the smallest power of two greater than or equal tox.static java.math.BigIntegerdivide(java.math.BigInteger p, java.math.BigInteger q, java.math.RoundingMode mode)Returns the result of dividingpbyq, rounding using the specifiedRoundingMode.static java.math.BigIntegerfactorial(int n)Returnsn!, that is, the product of the firstnpositive integers, or1ifn == 0.(package private) static booleanfitsInLong(java.math.BigInteger x)static java.math.BigIntegerfloorPowerOfTwo(java.math.BigInteger x)Returns the largest power of two less than or equal tox.static booleanisPowerOfTwo(java.math.BigInteger x)Returnstrueifxrepresents a power of two.(package private) static java.math.BigIntegerlistProduct(java.util.List<java.math.BigInteger> nums)(package private) static java.math.BigIntegerlistProduct(java.util.List<java.math.BigInteger> nums, int start, int end)static intlog10(java.math.BigInteger x, java.math.RoundingMode mode)Returns the base-10 logarithm ofx, rounded according to the specified rounding mode.static intlog2(java.math.BigInteger x, java.math.RoundingMode mode)Returns the base-2 logarithm ofx, rounded according to the specified rounding mode.static doubleroundToDouble(java.math.BigInteger x, java.math.RoundingMode mode)Returnsx, rounded to adoublewith the specified rounding mode.static java.math.BigIntegersqrt(java.math.BigInteger x, java.math.RoundingMode mode)Returns the square root ofx, rounded with the specified rounding mode.private static java.math.BigIntegersqrtApproxWithDoubles(java.math.BigInteger x)private static java.math.BigIntegersqrtFloor(java.math.BigInteger x)
-
-
-
Field Detail
-
SQRT2_PRECOMPUTE_THRESHOLD
static final int SQRT2_PRECOMPUTE_THRESHOLD
- See Also:
- Constant Field Values
-
SQRT2_PRECOMPUTED_BITS
static final java.math.BigInteger SQRT2_PRECOMPUTED_BITS
-
LN_10
private static final double LN_10
-
LN_2
private static final double LN_2
-
-
Method Detail
-
ceilingPowerOfTwo
public static java.math.BigInteger ceilingPowerOfTwo(java.math.BigInteger x)
Returns the smallest power of two greater than or equal tox. This is equivalent toBigInteger.valueOf(2).pow(log2(x, CEILING)).- Throws:
java.lang.IllegalArgumentException- ifx <= 0- Since:
- 20.0
-
floorPowerOfTwo
public static java.math.BigInteger floorPowerOfTwo(java.math.BigInteger x)
Returns the largest power of two less than or equal tox. This is equivalent toBigInteger.valueOf(2).pow(log2(x, FLOOR)).- Throws:
java.lang.IllegalArgumentException- ifx <= 0- Since:
- 20.0
-
isPowerOfTwo
public static boolean isPowerOfTwo(java.math.BigInteger x)
Returnstrueifxrepresents a power of two.
-
log2
public static int log2(java.math.BigInteger x, java.math.RoundingMode mode)Returns the base-2 logarithm ofx, rounded according to the specified rounding mode.- Throws:
java.lang.IllegalArgumentException- ifx <= 0java.lang.ArithmeticException- ifmodeisRoundingMode.UNNECESSARYandxis not a power of two
-
log10
public static int log10(java.math.BigInteger x, java.math.RoundingMode mode)Returns the base-10 logarithm ofx, rounded according to the specified rounding mode.- Throws:
java.lang.IllegalArgumentException- ifx <= 0java.lang.ArithmeticException- ifmodeisRoundingMode.UNNECESSARYandxis not a power of ten
-
sqrt
public static java.math.BigInteger sqrt(java.math.BigInteger x, java.math.RoundingMode mode)Returns the square root ofx, rounded with the specified rounding mode.- Throws:
java.lang.IllegalArgumentException- ifx < 0java.lang.ArithmeticException- ifmodeisRoundingMode.UNNECESSARYandsqrt(x)is not an integer
-
sqrtFloor
private static java.math.BigInteger sqrtFloor(java.math.BigInteger x)
-
sqrtApproxWithDoubles
private static java.math.BigInteger sqrtApproxWithDoubles(java.math.BigInteger x)
-
roundToDouble
public static double roundToDouble(java.math.BigInteger x, java.math.RoundingMode mode)Returnsx, rounded to adoublewith the specified rounding mode. Ifxis precisely representable as adouble, itsdoublevalue will be returned; otherwise, the rounding will choose between the two nearest representable values withmode.For the case of
RoundingMode.HALF_DOWN,HALF_UP, andHALF_EVEN, infinitedoublevalues are considered infinitely far away. For example, 2^2000 is not representable as a double, butroundToDouble(BigInteger.valueOf(2).pow(2000), HALF_UP)will returnDouble.MAX_VALUE, notDouble.POSITIVE_INFINITY.For the case of
RoundingMode.HALF_EVEN, this implementation uses the IEEE 754 default rounding mode: if the two nearest representable values are equally near, the one with the least significant bit zero is chosen. (In such cases, both of the nearest representable values are even integers; this method returns the one that is a multiple of a greater power of two.)- Throws:
java.lang.ArithmeticException- ifmodeisRoundingMode.UNNECESSARYandxis not precisely representable as adouble- Since:
- 30.0
-
divide
public static java.math.BigInteger divide(java.math.BigInteger p, java.math.BigInteger q, java.math.RoundingMode mode)Returns the result of dividingpbyq, rounding using the specifiedRoundingMode.- Throws:
java.lang.ArithmeticException- ifq == 0, or ifmode == UNNECESSARYandais not an integer multiple ofb
-
factorial
public static java.math.BigInteger factorial(int n)
Returnsn!, that is, the product of the firstnpositive integers, or1ifn == 0.Warning: the result takes O(n log n) space, so use cautiously.
This uses an efficient binary recursive algorithm to compute the factorial with balanced multiplies. It also removes all the 2s from the intermediate products (shifting them back in at the end).
- Throws:
java.lang.IllegalArgumentException- ifn < 0
-
listProduct
static java.math.BigInteger listProduct(java.util.List<java.math.BigInteger> nums)
-
listProduct
static java.math.BigInteger listProduct(java.util.List<java.math.BigInteger> nums, int start, int end)
-
binomial
public static java.math.BigInteger binomial(int n, int k)Returnsnchoosek, also known as the binomial coefficient ofnandk, that is,n! / (k! (n - k)!).Warning: the result can take as much as O(k log n) space.
- Throws:
java.lang.IllegalArgumentException- ifn < 0,k < 0, ork > n
-
fitsInLong
static boolean fitsInLong(java.math.BigInteger x)
-
-