Class ArithmeticUtils


  • public final class ArithmeticUtils
    extends Object
    Some useful, arithmetics related, additions to the built-in functions in Math.
    • Method Detail

      • addAndCheck

        public static int addAndCheck​(int x,
                                      int y)
                               throws MathArithmeticException
        Add two integers, checking for overflow.
        Parameters:
        x - an addend
        y - an addend
        Returns:
        the sum x+y
        Throws:
        MathArithmeticException - if the result can not be represented as an int.
        Since:
        1.1
      • addAndCheck

        public static long addAndCheck​(long a,
                                       long b)
                                throws MathArithmeticException
        Add two long integers, checking for overflow.
        Parameters:
        a - an addend
        b - an addend
        Returns:
        the sum a+b
        Throws:
        MathArithmeticException - if the result can not be represented as an long
        Since:
        1.2
      • factorialDouble

        @Deprecated
        public static double factorialDouble​(int n)
                                      throws NotPositiveException
        Compute n!, the factorial of n (the product of the numbers 1 to n), as a double. The result should be small enough to fit into a double: The largest n for which n! < Double.MAX_VALUE is 170. If the computed value exceeds Double.MAX_VALUE, Double.POSITIVE_INFINITY is returned.
        Parameters:
        n - Argument.
        Returns:
        n!
        Throws:
        NotPositiveException - if n < 0.
      • gcd

        public static int gcd​(int p,
                              int q)
                       throws MathArithmeticException
        Computes the greatest common divisor of the absolute value of two numbers, using a modified version of the "binary gcd" method. See Knuth 4.5.2 algorithm B. The algorithm is due to Josef Stein (1961).
        Special cases:
        • The invocations gcd(Integer.MIN_VALUE, Integer.MIN_VALUE), gcd(Integer.MIN_VALUE, 0) and gcd(0, Integer.MIN_VALUE) throw an ArithmeticException, because the result would be 2^31, which is too large for an int value.
        • The result of gcd(x, x), gcd(0, x) and gcd(x, 0) is the absolute value of x, except for the special cases above.
        • The invocation gcd(0, 0) is the only one which returns 0.
        Parameters:
        p - Number.
        q - Number.
        Returns:
        the greatest common divisor (never negative).
        Throws:
        MathArithmeticException - if the result cannot be represented as a non-negative int value.
        Since:
        1.1
      • gcd

        public static long gcd​(long p,
                               long q)
                        throws MathArithmeticException

        Gets the greatest common divisor of the absolute value of two numbers, using the "binary gcd" method which avoids division and modulo operations. See Knuth 4.5.2 algorithm B. This algorithm is due to Josef Stein (1961).

        Special cases:
        • The invocations gcd(Long.MIN_VALUE, Long.MIN_VALUE), gcd(Long.MIN_VALUE, 0L) and gcd(0L, Long.MIN_VALUE) throw an ArithmeticException, because the result would be 2^63, which is too large for a long value.
        • The result of gcd(x, x), gcd(0L, x) and gcd(x, 0L) is the absolute value of x, except for the special cases above.
        • The invocation gcd(0L, 0L) is the only one which returns 0L.
        Parameters:
        p - Number.
        q - Number.
        Returns:
        the greatest common divisor, never negative.
        Throws:
        MathArithmeticException - if the result cannot be represented as a non-negative long value.
        Since:
        2.1
      • lcm

        public static int lcm​(int a,
                              int b)
                       throws MathArithmeticException

        Returns the least common multiple of the absolute value of two numbers, using the formula lcm(a,b) = (a / gcd(a,b)) * b.

        Special cases:
        • The invocations lcm(Integer.MIN_VALUE, n) and lcm(n, Integer.MIN_VALUE), where abs(n) is a power of 2, throw an ArithmeticException, because the result would be 2^31, which is too large for an int value.
        • The result of lcm(0, x) and lcm(x, 0) is 0 for any x.
        Parameters:
        a - Number.
        b - Number.
        Returns:
        the least common multiple, never negative.
        Throws:
        MathArithmeticException - if the result cannot be represented as a non-negative int value.
        Since:
        1.1
      • lcm

        public static long lcm​(long a,
                               long b)
                        throws MathArithmeticException

        Returns the least common multiple of the absolute value of two numbers, using the formula lcm(a,b) = (a / gcd(a,b)) * b.

        Special cases:
        • The invocations lcm(Long.MIN_VALUE, n) and lcm(n, Long.MIN_VALUE), where abs(n) is a power of 2, throw an ArithmeticException, because the result would be 2^63, which is too large for an int value.
        • The result of lcm(0L, x) and lcm(x, 0L) is 0L for any x.
        Parameters:
        a - Number.
        b - Number.
        Returns:
        the least common multiple, never negative.
        Throws:
        MathArithmeticException - if the result cannot be represented as a non-negative long value.
        Since:
        2.1
      • mulAndCheck

        public static int mulAndCheck​(int x,
                                      int y)
                               throws MathArithmeticException
        Multiply two integers, checking for overflow.
        Parameters:
        x - Factor.
        y - Factor.
        Returns:
        the product x * y.
        Throws:
        MathArithmeticException - if the result can not be represented as an int.
        Since:
        1.1
      • mulAndCheck

        public static long mulAndCheck​(long a,
                                       long b)
                                throws MathArithmeticException
        Multiply two long integers, checking for overflow.
        Parameters:
        a - Factor.
        b - Factor.
        Returns:
        the product a * b.
        Throws:
        MathArithmeticException - if the result can not be represented as a long.
        Since:
        1.2
      • subAndCheck

        public static int subAndCheck​(int x,
                                      int y)
                               throws MathArithmeticException
        Subtract two integers, checking for overflow.
        Parameters:
        x - Minuend.
        y - Subtrahend.
        Returns:
        the difference x - y.
        Throws:
        MathArithmeticException - if the result can not be represented as an int.
        Since:
        1.1
      • subAndCheck

        public static long subAndCheck​(long a,
                                       long b)
                                throws MathArithmeticException
        Subtract two long integers, checking for overflow.
        Parameters:
        a - Value.
        b - Value.
        Returns:
        the difference a - b.
        Throws:
        MathArithmeticException - if the result can not be represented as a long.
        Since:
        1.2
      • isPowerOfTwo

        public static boolean isPowerOfTwo​(long n)
        Returns true if the argument is a power of two.
        Parameters:
        n - the number to test
        Returns:
        true if the argument is a power of two