Class BigIntegerMath
BigInteger.
 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 int and for long can be found in IntMath and
 LongMath respectively.
- Since:
- 11.0
- Author:
- Louis Wasserman
- 
Method SummaryModifier and TypeMethodDescriptionstatic BigIntegerbinomial(int n, int k) Returnsnchoosek, also known as the binomial coefficient ofnandk, that is,n! / (k! (n - k)!).static BigIntegerReturns the smallest power of two greater than or equal tox.static BigIntegerdivide(BigInteger p, BigInteger q, RoundingMode mode) Returns the result of dividingpbyq, rounding using the specifiedRoundingMode.static BigIntegerfactorial(int n) Returnsn!, that is, the product of the firstnpositive integers, or1ifn == 0.static BigIntegerReturns the largest power of two less than or equal tox.static booleanReturnstrueifxrepresents a power of two.static intlog10(BigInteger x, RoundingMode mode) Returns the base-10 logarithm ofx, rounded according to the specified rounding mode.static intlog2(BigInteger x, RoundingMode mode) Returns the base-2 logarithm ofx, rounded according to the specified rounding mode.static doubleroundToDouble(BigInteger x, RoundingMode mode) Returnsx, rounded to adoublewith the specified rounding mode.static BigIntegersqrt(BigInteger x, RoundingMode mode) Returns the square root ofx, rounded with the specified rounding mode.
- 
Method Details- 
ceilingPowerOfTwoReturns the smallest power of two greater than or equal tox. This is equivalent toBigInteger.valueOf(2).pow(log2(x, CEILING)).- Throws:
- IllegalArgumentException- if- x <= 0
- Since:
- 20.0
 
- 
floorPowerOfTwoReturns the largest power of two less than or equal tox. This is equivalent toBigInteger.valueOf(2).pow(log2(x, FLOOR)).- Throws:
- IllegalArgumentException- if- x <= 0
- Since:
- 20.0
 
- 
isPowerOfTwoReturnstrueifxrepresents a power of two.
- 
log2Returns the base-2 logarithm ofx, rounded according to the specified rounding mode.- Throws:
- IllegalArgumentException- if- x <= 0
- ArithmeticException- if- modeis- RoundingMode.UNNECESSARYand- xis not a power of two
 
- 
log10Returns the base-10 logarithm ofx, rounded according to the specified rounding mode.- Throws:
- IllegalArgumentException- if- x <= 0
- ArithmeticException- if- modeis- RoundingMode.UNNECESSARYand- xis not a power of ten
 
- 
sqrtReturns the square root ofx, rounded with the specified rounding mode.- Throws:
- IllegalArgumentException- if- x < 0
- ArithmeticException- if- modeis- RoundingMode.UNNECESSARYand- sqrt(x)is not an integer
 
- 
roundToDoubleReturnsx, 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:
- ArithmeticException- if- modeis- RoundingMode.UNNECESSARYand- xis not precisely representable as a- double
- Since:
- 30.0
 
- 
divideReturns the result of dividingpbyq, rounding using the specifiedRoundingMode.- Throws:
- ArithmeticException- if- q == 0, or if- mode == UNNECESSARYand- ais not an integer multiple of- b
 
- 
factorialReturnsn!, 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:
- IllegalArgumentException- if- n < 0
 
- 
binomialReturnsnchoosek, 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:
- IllegalArgumentException- if- n < 0,- k < 0, or- k > n
 
 
-