Package org.apache.commons.math3.util
Class ArithmeticUtils
- java.lang.Object
-
- org.apache.commons.math3.util.ArithmeticUtils
-
public final class ArithmeticUtils extends java.lang.ObjectSome useful, arithmetics related, additions to the built-in functions inMath.
-
-
Constructor Summary
Constructors Modifier Constructor Description privateArithmeticUtils()Private constructor.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static intaddAndCheck(int x, int y)Add two integers, checking for overflow.static longaddAndCheck(long a, long b)Add two long integers, checking for overflow.private static longaddAndCheck(long a, long b, Localizable pattern)Add two long integers, checking for overflow.static longbinomialCoefficient(int n, int k)Deprecated.static doublebinomialCoefficientDouble(int n, int k)Deprecated.static doublebinomialCoefficientLog(int n, int k)Deprecated.static longfactorial(int n)Deprecated.static doublefactorialDouble(int n)Deprecated.static doublefactorialLog(int n)Deprecated.static intgcd(int p, int q)Computes the greatest common divisor of the absolute value of two numbers, using a modified version of the "binary gcd" method.static longgcd(long p, long q)Gets the greatest common divisor of the absolute value of two numbers, using the "binary gcd" method which avoids division and modulo operations.private static intgcdPositive(int a, int b)Computes the greatest common divisor of two positive numbers (this precondition is not checked and the result is undefined if not fulfilled) using the "binary gcd" method which avoids division and modulo operations.static booleanisPowerOfTwo(long n)Returns true if the argument is a power of two.static intlcm(int a, int b)Returns the least common multiple of the absolute value of two numbers, using the formulalcm(a,b) = (a / gcd(a,b)) * b.static longlcm(long a, long b)Returns the least common multiple of the absolute value of two numbers, using the formulalcm(a,b) = (a / gcd(a,b)) * b.static intmulAndCheck(int x, int y)Multiply two integers, checking for overflow.static longmulAndCheck(long a, long b)Multiply two long integers, checking for overflow.static intpow(int k, int e)Raise an int to an int power.static intpow(int k, long e)Deprecated.As of 3.3.static longpow(long k, int e)Raise a long to an int power.static longpow(long k, long e)Deprecated.As of 3.3.static java.math.BigIntegerpow(java.math.BigInteger k, int e)Raise a BigInteger to an int power.static java.math.BigIntegerpow(java.math.BigInteger k, long e)Raise a BigInteger to a long power.static java.math.BigIntegerpow(java.math.BigInteger k, java.math.BigInteger e)Raise a BigInteger to a BigInteger power.static longstirlingS2(int n, int k)Deprecated.static intsubAndCheck(int x, int y)Subtract two integers, checking for overflow.static longsubAndCheck(long a, long b)Subtract two long integers, checking for overflow.
-
-
-
Method Detail
-
addAndCheck
public static int addAndCheck(int x, int y) throws MathArithmeticExceptionAdd two integers, checking for overflow.- Parameters:
x- an addendy- an addend- Returns:
- the sum
x+y - Throws:
MathArithmeticException- if the result can not be represented as anint.- Since:
- 1.1
-
addAndCheck
public static long addAndCheck(long a, long b) throws MathArithmeticExceptionAdd two long integers, checking for overflow.- Parameters:
a- an addendb- an addend- Returns:
- the sum
a+b - Throws:
MathArithmeticException- if the result can not be represented as an long- Since:
- 1.2
-
binomialCoefficient
@Deprecated public static long binomialCoefficient(int n, int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticExceptionDeprecated.Returns an exact representation of the Binomial Coefficient, "n choose k", the number ofk-element subsets that can be selected from ann-element set.Preconditions:
-
0 <= k <= n(otherwiseIllegalArgumentExceptionis thrown) - The result is small enough to fit into a
long. The largest value ofnfor which all coefficients are< Long.MAX_VALUEis 66. If the computed value exceedsLong.MAX_VALUEanArithMeticExceptionis thrown.
- Parameters:
n- the size of the setk- the size of the subsets to be counted- Returns:
n choose k- Throws:
NotPositiveException- ifn < 0.NumberIsTooLargeException- ifk > n.MathArithmeticException- if the result is too large to be represented by a long integer.
-
-
binomialCoefficientDouble
@Deprecated public static double binomialCoefficientDouble(int n, int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticExceptionDeprecated.Returns adoublerepresentation of the Binomial Coefficient, "n choose k", the number ofk-element subsets that can be selected from ann-element set.Preconditions:
-
0 <= k <= n(otherwiseIllegalArgumentExceptionis thrown) - The result is small enough to fit into a
double. The largest value ofnfor which all coefficients are < Double.MAX_VALUE is 1029. If the computed value exceeds Double.MAX_VALUE, Double.POSITIVE_INFINITY is returned
- Parameters:
n- the size of the setk- the size of the subsets to be counted- Returns:
n choose k- Throws:
NotPositiveException- ifn < 0.NumberIsTooLargeException- ifk > n.MathArithmeticException- if the result is too large to be represented by a long integer.
-
-
binomialCoefficientLog
@Deprecated public static double binomialCoefficientLog(int n, int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticExceptionDeprecated.Returns the naturallogof the Binomial Coefficient, "n choose k", the number ofk-element subsets that can be selected from ann-element set.Preconditions:
-
0 <= k <= n(otherwiseIllegalArgumentExceptionis thrown)
- Parameters:
n- the size of the setk- the size of the subsets to be counted- Returns:
n choose k- Throws:
NotPositiveException- ifn < 0.NumberIsTooLargeException- ifk > n.MathArithmeticException- if the result is too large to be represented by a long integer.
-
-
factorial
@Deprecated public static long factorial(int n) throws NotPositiveException, MathArithmeticExceptionDeprecated.Returns n!. Shorthand fornFactorial, the product of the numbers1,...,n.Preconditions:
-
n >= 0(otherwiseIllegalArgumentExceptionis thrown) - The result is small enough to fit into a
long. The largest value ofnfor whichn!< Long.MAX_VALUE} is 20. If the computed value exceedsLong.MAX_VALUEanArithMeticExceptionis thrown.
- Parameters:
n- argument- Returns:
n!- Throws:
MathArithmeticException- if the result is too large to be represented by along.NotPositiveException- ifn < 0.MathArithmeticException- ifn > 20: The factorial value is too large to fit in along.
-
-
factorialDouble
@Deprecated public static double factorialDouble(int n) throws NotPositiveExceptionDeprecated.Compute n!, the factorial ofn(the product of the numbers 1 to n), as adouble. The result should be small enough to fit into adouble: The largestnfor whichn! < Double.MAX_VALUEis 170. If the computed value exceedsDouble.MAX_VALUE,Double.POSITIVE_INFINITYis returned.- Parameters:
n- Argument.- Returns:
n!- Throws:
NotPositiveException- ifn < 0.
-
factorialLog
@Deprecated public static double factorialLog(int n) throws NotPositiveExceptionDeprecated.Compute the natural logarithm of the factorial ofn.- Parameters:
n- Argument.- Returns:
n!- Throws:
NotPositiveException- ifn < 0.
-
gcd
public static int gcd(int p, int q) throws MathArithmeticExceptionComputes 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)andgcd(0, Integer.MIN_VALUE)throw anArithmeticException, because the result would be 2^31, which is too large for an int value. - The result of
gcd(x, x),gcd(0, x)andgcd(x, 0)is the absolute value ofx, except for the special cases above. - The invocation
gcd(0, 0)is the only one which returns0.
- Parameters:
p- Number.q- Number.- Returns:
- the greatest common divisor (never negative).
- Throws:
MathArithmeticException- if the result cannot be represented as a non-negativeintvalue.- Since:
- 1.1
- The invocations
-
gcdPositive
private static int gcdPositive(int a, int b)Computes the greatest common divisor of two positive numbers (this precondition is not checked and the result is undefined if not fulfilled) using the "binary gcd" method which avoids division and modulo operations. See Knuth 4.5.2 algorithm B. The algorithm is due to Josef Stein (1961).
Special cases:- The result of
gcd(x, x),gcd(0, x)andgcd(x, 0)is the value ofx. - The invocation
gcd(0, 0)is the only one which returns0.
- Parameters:
a- Positive number.b- Positive number.- Returns:
- the greatest common divisor.
- The result of
-
gcd
public static long gcd(long p, long q) throws MathArithmeticExceptionGets 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)andgcd(0L, Long.MIN_VALUE)throw anArithmeticException, because the result would be 2^63, which is too large for a long value. - The result of
gcd(x, x),gcd(0L, x)andgcd(x, 0L)is the absolute value ofx, except for the special cases above. - The invocation
gcd(0L, 0L)is the only one which returns0L.
- Parameters:
p- Number.q- Number.- Returns:
- the greatest common divisor, never negative.
- Throws:
MathArithmeticException- if the result cannot be represented as a non-negativelongvalue.- Since:
- 2.1
- The invocations
-
lcm
public static int lcm(int a, int b) throws MathArithmeticExceptionReturns the least common multiple of the absolute value of two numbers, using the formula
Special cases:lcm(a,b) = (a / gcd(a,b)) * b.- The invocations
lcm(Integer.MIN_VALUE, n)andlcm(n, Integer.MIN_VALUE), whereabs(n)is a power of 2, throw anArithmeticException, because the result would be 2^31, which is too large for an int value. - The result of
lcm(0, x)andlcm(x, 0)is0for anyx.
- Parameters:
a- Number.b- Number.- Returns:
- the least common multiple, never negative.
- Throws:
MathArithmeticException- if the result cannot be represented as a non-negativeintvalue.- Since:
- 1.1
- The invocations
-
lcm
public static long lcm(long a, long b) throws MathArithmeticExceptionReturns the least common multiple of the absolute value of two numbers, using the formula
Special cases:lcm(a,b) = (a / gcd(a,b)) * b.- The invocations
lcm(Long.MIN_VALUE, n)andlcm(n, Long.MIN_VALUE), whereabs(n)is a power of 2, throw anArithmeticException, because the result would be 2^63, which is too large for an int value. - The result of
lcm(0L, x)andlcm(x, 0L)is0Lfor anyx.
- Parameters:
a- Number.b- Number.- Returns:
- the least common multiple, never negative.
- Throws:
MathArithmeticException- if the result cannot be represented as a non-negativelongvalue.- Since:
- 2.1
- The invocations
-
mulAndCheck
public static int mulAndCheck(int x, int y) throws MathArithmeticExceptionMultiply 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 anint.- Since:
- 1.1
-
mulAndCheck
public static long mulAndCheck(long a, long b) throws MathArithmeticExceptionMultiply 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 along.- Since:
- 1.2
-
subAndCheck
public static int subAndCheck(int x, int y) throws MathArithmeticExceptionSubtract 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 anint.- Since:
- 1.1
-
subAndCheck
public static long subAndCheck(long a, long b) throws MathArithmeticExceptionSubtract 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 along.- Since:
- 1.2
-
pow
public static int pow(int k, int e) throws NotPositiveException, MathArithmeticExceptionRaise an int to an int power.- Parameters:
k- Number to raise.e- Exponent (must be positive or zero).- Returns:
- \( k^e \)
- Throws:
NotPositiveException- ife < 0.MathArithmeticException- if the result would overflow.
-
pow
@Deprecated public static int pow(int k, long e) throws NotPositiveExceptionDeprecated.As of 3.3. Please usepow(int,int)instead.Raise an int to a long power.- Parameters:
k- Number to raise.e- Exponent (must be positive or zero).- Returns:
- ke
- Throws:
NotPositiveException- ife < 0.
-
pow
public static long pow(long k, int e) throws NotPositiveException, MathArithmeticExceptionRaise a long to an int power.- Parameters:
k- Number to raise.e- Exponent (must be positive or zero).- Returns:
- \( k^e \)
- Throws:
NotPositiveException- ife < 0.MathArithmeticException- if the result would overflow.
-
pow
@Deprecated public static long pow(long k, long e) throws NotPositiveExceptionDeprecated.As of 3.3. Please usepow(long,int)instead.Raise a long to a long power.- Parameters:
k- Number to raise.e- Exponent (must be positive or zero).- Returns:
- ke
- Throws:
NotPositiveException- ife < 0.
-
pow
public static java.math.BigInteger pow(java.math.BigInteger k, int e) throws NotPositiveExceptionRaise a BigInteger to an int power.- Parameters:
k- Number to raise.e- Exponent (must be positive or zero).- Returns:
- ke
- Throws:
NotPositiveException- ife < 0.
-
pow
public static java.math.BigInteger pow(java.math.BigInteger k, long e) throws NotPositiveExceptionRaise a BigInteger to a long power.- Parameters:
k- Number to raise.e- Exponent (must be positive or zero).- Returns:
- ke
- Throws:
NotPositiveException- ife < 0.
-
pow
public static java.math.BigInteger pow(java.math.BigInteger k, java.math.BigInteger e) throws NotPositiveExceptionRaise a BigInteger to a BigInteger power.- Parameters:
k- Number to raise.e- Exponent (must be positive or zero).- Returns:
- ke
- Throws:
NotPositiveException- ife < 0.
-
stirlingS2
@Deprecated public static long stirlingS2(int n, int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticExceptionDeprecated.Returns the Stirling number of the second kind, "S(n,k)", the number of ways of partitioning ann-element set intoknon-empty subsets.The preconditions are
0 <= k <= n(otherwiseNotPositiveExceptionis thrown)- Parameters:
n- the size of the setk- the number of non-empty subsets- Returns:
S(n,k)- Throws:
NotPositiveException- ifk < 0.NumberIsTooLargeException- ifk > n.MathArithmeticException- if some overflow happens, typically for n exceeding 25 and k between 20 and n-2 (S(n,n-1) is handled specifically and does not overflow)- Since:
- 3.1
-
addAndCheck
private static long addAndCheck(long a, long b, Localizable pattern) throws MathArithmeticExceptionAdd two long integers, checking for overflow.- Parameters:
a- Addend.b- Addend.pattern- Pattern to use for any thrown exception.- Returns:
- the sum
a + b. - Throws:
MathArithmeticException- if the result cannot be represented as along.- 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
-
-