Class BigIntegerMath
-
- com.google.common.math.BigIntegerMath
-
@GwtCompatible(emulated=true) public final class BigIntegerMath extends
A 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
int
and forlong
can be found inIntMath
andLongMath
respectively.- Since:
- 11.0
- Author:
- Louis Wasserman
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static
binomial(int n, int k)
Returnsn
choosek
, also known as the binomial coefficient ofn
andk
, that is,n! / (k! (n - k)!)
.static
x)
Returns the smallest power of two greater than or equal tox
.static
p, q, mode)
Returns the result of dividingp
byq
, rounding using the specifiedRoundingMode
.static
factorial(int n)
Returnsn!
, that is, the product of the firstn
positive integers, or1
ifn == 0
.static
x)
Returns the largest power of two less than or equal tox
.static boolean
x)
Returnstrue
ifx
represents a power of two.static int
x, mode)
Returns the base-10 logarithm ofx
, rounded according to the specified rounding mode.static int
x, mode)
Returns the base-2 logarithm ofx
, rounded according to the specified rounding mode.static double
x, mode)
Returnsx
, rounded to adouble
with the specified rounding mode.static
x, mode)
Returns the square root ofx
, rounded with the specified rounding mode.
-
-
-
Method Detail
-
ceilingPowerOfTwo
@Beta public static x)
Returns the smallest power of two greater than or equal tox
. This is equivalent toBigInteger.valueOf(2).pow(log2(x, CEILING))
.- Throws:
- if
x <= 0
- Since:
- 20.0
-
floorPowerOfTwo
@Beta public static x)
Returns the largest power of two less than or equal tox
. This is equivalent toBigInteger.valueOf(2).pow(log2(x, FLOOR))
.- Throws:
- if
x <= 0
- Since:
- 20.0
-
isPowerOfTwo
public static boolean x)
Returnstrue
ifx
represents a power of two.
-
log2
public static int x, mode)
Returns the base-2 logarithm ofx
, rounded according to the specified rounding mode.- Throws:
- if
x <= 0
- if
mode
is andx
is not a power of two
-
log10
@GwtIncompatible public static int x, mode)
Returns the base-10 logarithm ofx
, rounded according to the specified rounding mode.- Throws:
- if
x <= 0
- if
mode
is andx
is not a power of ten
-
sqrt
@GwtIncompatible public static x, mode)
Returns the square root ofx
, rounded with the specified rounding mode.- Throws:
- if
x < 0
- if
mode
is andsqrt(x)
is not an integer
-
roundToDouble
@GwtIncompatible public static double x, mode)
Returnsx
, rounded to adouble
with the specified rounding mode. Ifx
is precisely representable as adouble
, itsdouble
value will be returned; otherwise, the rounding will choose between the two nearest representable values withmode
.For the case of ,
HALF_UP
, andHALF_EVEN
, infinitedouble
values 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 , 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:
- if
mode
is andx
is not precisely representable as adouble
- Since:
- 30.0
-
divide
@GwtIncompatible public static p, q, mode)
Returns the result of dividingp
byq
, rounding using the specifiedRoundingMode
.- Throws:
- if
q == 0
, or ifmode == UNNECESSARY
anda
is not an integer multiple ofb
-
factorial
public static factorial(int n)
Returnsn!
, that is, the product of the firstn
positive integers, or1
ifn == 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:
- if
n < 0
-
binomial
public static binomial(int n, int k)
Returnsn
choosek
, also known as the binomial coefficient ofn
andk
, that is,n! / (k! (n - k)!)
.Warning: the result can take as much as O(k log n) space.
- Throws:
- if
n < 0
,k < 0
, ork > n
-
-