Top Description Inners Fields Constructors Methods
com.google.common.math

public final Class BigIntegerMath

extends Object
Class Inheritance
Annotations
@GwtCompatible
emulated:true
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, com.google.common.math.MathPreconditions.checkNonNegative, .MathPreconditions.checkPositive, .MathPreconditions.checkRoundingUnnecessary, java.math.RoundingMode.CEILING, .RoundingMode.FLOOR, .RoundingMode.HALF_EVEN

A class for arithmetic on values of type 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.

Author
Louis Wasserman
Since
11.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static final double
private static final double
pack-priv static final int
pack-priv static final BigInteger

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static BigInteger
binomial(int n, int k)

Returns n choose k, also known as the binomial coefficient of n and k, that is, n!

public static BigInteger
ceilingPowerOfTwo(BigInteger x)

Returns the smallest power of two greater than or equal to x.

public static BigInteger
divide(BigInteger p, BigInteger q, RoundingMode mode)

Returns the result of dividing p by q, rounding using the specified RoundingMode.

public static BigInteger
factorial(int n)

Returns n!

pack-priv static boolean
public static BigInteger
floorPowerOfTwo(BigInteger x)

Returns the largest power of two less than or equal to x.

public static boolean
isPowerOfTwo(BigInteger x)

Returns true if x represents a power of two.

pack-priv static BigInteger
pack-priv static BigInteger
listProduct(List<BigInteger> nums, int start, int end)

public static int
log10(BigInteger x, RoundingMode mode)

Returns the base-10 logarithm of x, rounded according to the specified rounding mode.

public static int
log2(BigInteger x, RoundingMode mode)

Returns the base-2 logarithm of x, rounded according to the specified rounding mode.

public static double
roundToDouble(BigInteger x, RoundingMode mode)

Returns x, rounded to a double with the specified rounding mode.

public static BigInteger
sqrt(BigInteger x, RoundingMode mode)

Returns the square root of x, rounded with the specified rounding mode.

private static BigInteger
private static BigInteger
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

LN_10back to summary
private static final double LN_10
LN_2back to summary
private static final double LN_2
SQRT2_PRECOMPUTE_THRESHOLDback to summary
pack-priv static final int SQRT2_PRECOMPUTE_THRESHOLD
Annotations
@VisibleForTesting
SQRT2_PRECOMPUTED_BITSback to summary
pack-priv static final BigInteger SQRT2_PRECOMPUTED_BITS
Annotations
@VisibleForTesting

Constructor Detail

BigIntegerMathback to summary
private BigIntegerMath()

Method Detail

binomialback to summary
public static BigInteger binomial(int n, int k)

Returns n choose k, also known as the binomial coefficient of n and k, that is, n! / (k! (n - k)!).

Warning

the result can take as much as O(k log n) space.

Exceptions
IllegalArgumentException:
if n < 0, k < 0, or k > n
ceilingPowerOfTwoback to summary
public static BigInteger ceilingPowerOfTwo(BigInteger x)

Returns the smallest power of two greater than or equal to x. This is equivalent to BigInteger.valueOf(2).pow(log2(x, CEILING)).

Exceptions
IllegalArgumentException:
if x <= 0
Since
20.0
divideback to summary
public static BigInteger divide(BigInteger p, BigInteger q, RoundingMode mode)

Returns the result of dividing p by q, rounding using the specified RoundingMode.

Annotations
@GwtIncompatible
Exceptions
ArithmeticException:
if q == 0, or if mode == UNNECESSARY and a is not an integer multiple of b
factorialback to summary
public static BigInteger factorial(int n)

Returns n!, that is, the product of the first n positive integers, or 1 if n == 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).

Exceptions
IllegalArgumentException:
if n < 0
fitsInLongback to summary
pack-priv static boolean fitsInLong(BigInteger x)
Annotations
@GwtIncompatible
floorPowerOfTwoback to summary
public static BigInteger floorPowerOfTwo(BigInteger x)

Returns the largest power of two less than or equal to x. This is equivalent to BigInteger.valueOf(2).pow(log2(x, FLOOR)).

Exceptions
IllegalArgumentException:
if x <= 0
Since
20.0
isPowerOfTwoback to summary
public static boolean isPowerOfTwo(BigInteger x)

Returns true if x represents a power of two.

listProductback to summary
pack-priv static BigInteger listProduct(List<BigInteger> nums)
listProductback to summary
pack-priv static BigInteger listProduct(List<BigInteger> nums, int start, int end)
log10back to summary
public static int log10(BigInteger x, RoundingMode mode)

Returns the base-10 logarithm of x, rounded according to the specified rounding mode.

Annotations
@GwtIncompatible
@SuppressWarnings:fallthrough
Exceptions
IllegalArgumentException:
if x <= 0
ArithmeticException:
if mode is RoundingMode#UNNECESSARY and x is not a power of ten
log2back to summary
public static int log2(BigInteger x, RoundingMode mode)

Returns the base-2 logarithm of x, rounded according to the specified rounding mode.

Annotations
@SuppressWarnings:fallthrough
Exceptions
IllegalArgumentException:
if x <= 0
ArithmeticException:
if mode is RoundingMode#UNNECESSARY and x is not a power of two
roundToDoubleback to summary
public static double roundToDouble(BigInteger x, RoundingMode mode)

Returns x, rounded to a double with the specified rounding mode. If x is precisely representable as a double, its double value will be returned; otherwise, the rounding will choose between the two nearest representable values with mode.

For the case of RoundingMode#HALF_DOWN, HALF_UP, and HALF_EVEN, infinite double values are considered infinitely far away. For example, 2^2000 is not representable as a double, but roundToDouble(BigInteger.valueOf(2).pow(2000), HALF_UP) will return Double.MAX_VALUE, not Double.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.)

Annotations
@GwtIncompatible
Exceptions
ArithmeticException:
if mode is RoundingMode#UNNECESSARY and x is not precisely representable as a double
Since
30.0
sqrtback to summary
public static BigInteger sqrt(BigInteger x, RoundingMode mode)

Returns the square root of x, rounded with the specified rounding mode.

Annotations
@GwtIncompatible
@SuppressWarnings:fallthrough
Exceptions
IllegalArgumentException:
if x < 0
ArithmeticException:
if mode is RoundingMode#UNNECESSARY and sqrt(x) is not an integer
sqrtApproxWithDoublesback to summary
private static BigInteger sqrtApproxWithDoubles(BigInteger x)
Annotations
@GwtIncompatible
sqrtFloorback to summary
private static BigInteger sqrtFloor(BigInteger x)
Annotations
@GwtIncompatible
com.google.common.math back to summary

private Class BigIntegerMath.BigIntegerToDoubleRounder

extends ToDoubleRounder<BigInteger>
Class Inheritance
Annotations
@GwtIncompatible

Field Summary

Modifier and TypeField and Description
pack-priv static final BigIntegerMath.BigIntegerToDoubleRounder

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv BigInteger
minus(BigInteger a, BigInteger b)

Implements abstract com.google.common.math.ToDoubleRounder.minus.

Returns a - b, guaranteed that both arguments are nonnegative.
pack-priv double
roundToDoubleArbitrarily(BigInteger bigInteger)

Implements abstract com.google.common.math.ToDoubleRounder.roundToDoubleArbitrarily.

Returns x rounded to either the greatest double less than or equal to the precise value of x, or the least double greater than or equal to the precise value of x.
pack-priv int
sign(BigInteger bigInteger)

Implements abstract com.google.common.math.ToDoubleRounder.sign.

Returns the sign of x: either -1, 0, or 1.
pack-priv BigInteger
toX(double d, RoundingMode mode)

Implements abstract com.google.common.math.ToDoubleRounder.toX.

Returns d's value as an X, rounded with the specified mode.
Inherited from com.google.common.math.ToDoubleRounder:
roundToDouble

Field Detail

INSTANCEback to summary
pack-priv static final BigIntegerMath.BigIntegerToDoubleRounder INSTANCE

Constructor Detail

BigIntegerToDoubleRounderback to summary
private BigIntegerToDoubleRounder()

Method Detail

minusback to summary
pack-priv BigInteger minus(BigInteger a, BigInteger b)

Implements abstract com.google.common.math.ToDoubleRounder.minus.

Doc from com.google.common.math.ToDoubleRounder.minus.

Returns a - b, guaranteed that both arguments are nonnegative.

Annotations
@Override
roundToDoubleArbitrarilyback to summary
pack-priv double roundToDoubleArbitrarily(BigInteger bigInteger)

Implements abstract com.google.common.math.ToDoubleRounder.roundToDoubleArbitrarily.

Doc from com.google.common.math.ToDoubleRounder.roundToDoubleArbitrarily.

Returns x rounded to either the greatest double less than or equal to the precise value of x, or the least double greater than or equal to the precise value of x.

Annotations
@Override
signback to summary
pack-priv int sign(BigInteger bigInteger)

Implements abstract com.google.common.math.ToDoubleRounder.sign.

Doc from com.google.common.math.ToDoubleRounder.sign.

Returns the sign of x: either -1, 0, or 1.

Annotations
@Override
toXback to summary
pack-priv BigInteger toX(double d, RoundingMode mode)

Implements abstract com.google.common.math.ToDoubleRounder.toX.

Doc from com.google.common.math.ToDoubleRounder.toX.

Returns d's value as an X, rounded with the specified mode.

Annotations
@Override