Class BigIntegerMath

java.lang.Object
com.google.common.math.BigIntegerMath

public final class BigIntegerMath extends Object
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.

Since:
11.0
  • Field Details

    • SQRT2_PRECOMPUTE_THRESHOLD

      static final int SQRT2_PRECOMPUTE_THRESHOLD
      See Also:
    • SQRT2_PRECOMPUTED_BITS

      static final BigInteger SQRT2_PRECOMPUTED_BITS
    • LN_10

      private static final double LN_10
    • LN_2

      private static final double LN_2
  • Constructor Details

    • BigIntegerMath

      private BigIntegerMath()
  • Method Details

    • ceilingPowerOfTwo

      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)).
      Throws:
      IllegalArgumentException - if x <= 0
      Since:
      20.0
    • floorPowerOfTwo

      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)).
      Throws:
      IllegalArgumentException - if x <= 0
      Since:
      20.0
    • isPowerOfTwo

      public static boolean isPowerOfTwo(BigInteger x)
      Returns true if x represents a power of two.
    • log2

      public static int log2(BigInteger x, RoundingMode mode)
      Returns the base-2 logarithm of x, rounded according to the specified rounding mode.
      Throws:
      IllegalArgumentException - if x <= 0
      ArithmeticException - if mode is RoundingMode.UNNECESSARY and x is not a power of two
    • log10

      public static int log10(BigInteger x, RoundingMode mode)
      Returns the base-10 logarithm of x, rounded according to the specified rounding mode.
      Throws:
      IllegalArgumentException - if x <= 0
      ArithmeticException - if mode is RoundingMode.UNNECESSARY and x is not a power of ten
    • sqrt

      public static BigInteger sqrt(BigInteger x, RoundingMode mode)
      Returns the square root of x, rounded with the specified rounding mode.
      Throws:
      IllegalArgumentException - if x < 0
      ArithmeticException - if mode is RoundingMode.UNNECESSARY and sqrt(x) is not an integer
    • sqrtFloor

      private static BigInteger sqrtFloor(BigInteger x)
    • sqrtApproxWithDoubles

      private static BigInteger sqrtApproxWithDoubles(BigInteger x)
    • roundToDouble

      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.)

      Throws:
      ArithmeticException - if mode is RoundingMode.UNNECESSARY and x is not precisely representable as a double
      Since:
      30.0
    • divide

      public static BigInteger divide(BigInteger p, BigInteger q, RoundingMode mode)
      Returns the result of dividing p by q, rounding using the specified RoundingMode.
      Throws:
      ArithmeticException - if q == 0, or if mode == UNNECESSARY and a is not an integer multiple of b
    • factorial

      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).

      Throws:
      IllegalArgumentException - if n < 0
    • listProduct

      static BigInteger listProduct(List<BigInteger> nums)
    • listProduct

      static BigInteger listProduct(List<BigInteger> nums, int start, int end)
    • binomial

      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.

      Throws:
      IllegalArgumentException - if n < 0, k < 0, or k > n
    • fitsInLong

      static boolean fitsInLong(BigInteger x)