Class MathUtils


  • public final class MathUtils
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static long checkedMultiply​(long a, long b)
      Returns the product of a and b, provided it does not overflow.
      static long checkedPow​(long b, int k)
      Returns the b to the kth power, provided it does not overflow.
      static long gcd​(long a, long b)
      Returns the greatest common divisor of a, b.
      static boolean isMathematicalInteger​(double x)  
      static boolean isPowerOfTwo​(double x)
      Returns true if x is exactly equal to 2^k for some finite integer k.
      static boolean isPowerOfTwo​(long x)
      Returns true if x represents a power of two.
      static long roundToLong​(double x, java.math.RoundingMode mode)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • roundToLong

        public static long roundToLong​(double x,
                                       java.math.RoundingMode mode)
      • isMathematicalInteger

        public static boolean isMathematicalInteger​(double x)
      • isPowerOfTwo

        public static boolean isPowerOfTwo​(long x)
        Returns true if x represents a power of two.

        This differs from Long.bitCount(x) == 1, because Long.bitCount(Long.MIN_VALUE) == 1, but Long.MIN_VALUE is not a power of two.

      • isPowerOfTwo

        public static boolean isPowerOfTwo​(double x)
        Returns true if x is exactly equal to 2^k for some finite integer k.
      • checkedPow

        public static long checkedPow​(long b,
                                      int k)
        Returns the b to the kth power, provided it does not overflow.
        Throws:
        java.lang.ArithmeticException - if b to the kth power overflows in signed long arithmetic
      • gcd

        public static long gcd​(long a,
                               long b)
        Returns the greatest common divisor of a, b. Returns 0 if a == 0 && b == 0.
        Throws:
        java.lang.IllegalArgumentException - if a < 0 or b < 0
      • checkedMultiply

        public static long checkedMultiply​(long a,
                                           long b)
        Returns the product of a and b, provided it does not overflow.
        Throws:
        java.lang.ArithmeticException - if a * b overflows in signed long arithmetic