Top Description Constructors Methods
org.apache.commons.math3.util

public final Class ArithmeticUtils

extends Object
Class Inheritance
Imports
java.math.BigInteger, org.apache.commons.math3.exception.MathArithmeticException, .NotPositiveException, .NumberIsTooLargeException, org.apache.commons.math3.exception.util.Localizable, .LocalizedFormats

Some useful, arithmetics related, additions to the built-in functions in Math.

Constructor Summary

AccessConstructor and Description
private
ArithmeticUtils()

Private constructor.

Method Summary

Modifier and TypeMethod and Description
public static int

Returns:

the sum x+y
addAndCheck
(int
an addend
x
,
int
an addend
y
)

Add two integers, checking for overflow.

public static long

Returns:

the sum a+b
addAndCheck
(long
an addend
a
,
long
an addend
b
)

Add two long integers, checking for overflow.

private static long

Returns:

the sum a + b.
addAndCheck
(long
Addend.
a
,
long
Addend.
b
,
Localizable
Pattern to use for any thrown exception.
pattern
)

Add two long integers, checking for overflow.

public static long

Returns:

n choose k
binomialCoefficient
(final int
the size of the set
n
,
final int
the size of the subsets to be counted
k
)

Returns an exact representation of the Binomial Coefficient, "n choose k", the number of k-element subsets that can be selected from an n-element set.

public static double

Returns:

n choose k
binomialCoefficientDouble
(final int
the size of the set
n
,
final int
the size of the subsets to be counted
k
)

Returns a double representation of the Binomial Coefficient, "n choose k", the number of k-element subsets that can be selected from an n-element set.

public static double

Returns:

n choose k
binomialCoefficientLog
(final int
the size of the set
n
,
final int
the size of the subsets to be counted
k
)

Returns the natural log of the Binomial Coefficient, "n choose k", the number of k-element subsets that can be selected from an n-element set.

public static long

Returns:

n!
factorial
(final int
argument
n
)

Returns n!.

public static double

Returns:

n!
factorialDouble
(final int
Argument.
n
)

Compute n!, the factorial of n (the product of the numbers 1 to n), as a double.

public static double

Returns:

n!
factorialLog
(final int
Argument.
n
)

Compute the natural logarithm of the factorial of n.

public static int

Returns:

the greatest common divisor (never negative).
gcd
(int
Number.
p
,
int
Number.
q
)

Computes the greatest common divisor of the absolute value of two numbers, using a modified version of the "binary gcd" method.

public static long

Returns:

the greatest common divisor, never negative.
gcd
(final long
Number.
p
,
final long
Number.
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 int

Returns:

the greatest common divisor.
gcdPositive
(int
Positive number.
a
,
int
Positive number.
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.

public static boolean

Returns:

true if the argument is a power of two
isPowerOfTwo
(long
the number to test
n
)

Returns true if the argument is a power of two.

public static int

Returns:

the least common multiple, never negative.
lcm
(int
Number.
a
,
int
Number.
b
)

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

public static long

Returns:

the least common multiple, never negative.
lcm
(long
Number.
a
,
long
Number.
b
)

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

public static int

Returns:

the product x * y.
mulAndCheck
(int
Factor.
x
,
int
Factor.
y
)

Multiply two integers, checking for overflow.

public static long

Returns:

the product a * b.
mulAndCheck
(long
Factor.
a
,
long
Factor.
b
)

Multiply two long integers, checking for overflow.

public static int

Returns:

\( k^e \)
pow
(final int
Number to raise.
k
,
final int
Exponent (must be positive or zero).
e
)

Raise an int to an int power.

public static int

Returns:

ke
pow
(final int
Number to raise.
k
,
long
Exponent (must be positive or zero).
e
)
Deprecated As of 3.3. Please use pow(int, int) instead.

Raise an int to a long power.

public static long

Returns:

\( k^e \)
pow
(final long
Number to raise.
k
,
final int
Exponent (must be positive or zero).
e
)

Raise a long to an int power.

public static long

Returns:

ke
pow
(final long
Number to raise.
k
,
long
Exponent (must be positive or zero).
e
)
Deprecated As of 3.3. Please use pow(long, int) instead.

Raise a long to a long power.

public static BigInteger

Returns:

ke
pow
(final BigInteger
Number to raise.
k
,
int
Exponent (must be positive or zero).
e
)

Raise a BigInteger to an int power.

public static BigInteger

Returns:

ke
pow
(final BigInteger
Number to raise.
k
,
long
Exponent (must be positive or zero).
e
)

Raise a BigInteger to a long power.

public static BigInteger

Returns:

ke
pow
(final BigInteger
Number to raise.
k
,
BigInteger
Exponent (must be positive or zero).
e
)

Raise a BigInteger to a BigInteger power.

public static long

Returns:

S(n,k)
stirlingS2
(final int
the size of the set
n
,
final int
the number of non-empty subsets
k
)

Returns the Stirling number of the second kind, "S(n,k)", the number of ways of partitioning an n-element set into k non-empty subsets.

public static int

Returns:

the difference x - y.
subAndCheck
(int
Minuend.
x
,
int
Subtrahend.
y
)

Subtract two integers, checking for overflow.

public static long

Returns:

the difference a - b.
subAndCheck
(long
Value.
a
,
long
Value.
b
)

Subtract two long integers, checking for overflow.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ArithmeticUtilsback to summary
private ArithmeticUtils()

Private constructor.

Method Detail

addAndCheckback to summary
public static int addAndCheck(int x, int y) throws MathArithmeticException

Add two integers, checking for overflow.

Parameters
x:int

an addend

y:int

an addend

Returns:int

the sum x+y

Exceptions
MathArithmeticException:
if the result can not be represented as an int.
Since
1.1
addAndCheckback to summary
public static long addAndCheck(long a, long b) throws MathArithmeticException

Add two long integers, checking for overflow.

Parameters
a:long

an addend

b:long

an addend

Returns:long

the sum a+b

Exceptions
MathArithmeticException:
if the result can not be represented as an long
Since
1.2
addAndCheckback to summary
private static long addAndCheck(long a, long b, Localizable pattern) throws MathArithmeticException

Add two long integers, checking for overflow.

Parameters
a:long

Addend.

b:long

Addend.

pattern:Localizable

Pattern to use for any thrown exception.

Returns:long

the sum a + b.

Exceptions
MathArithmeticException:
if the result cannot be represented as a long.
Since
1.2
binomialCoefficientback to summary
public static long binomialCoefficient(final int n, final int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticException

Returns an exact representation of the Binomial Coefficient, "n choose k", the number of k-element subsets that can be selected from an n-element set.

Preconditions:

  • 0 <= k <= n (otherwise IllegalArgumentException is thrown)
  • The result is small enough to fit into a long. The largest value of n for which all coefficients are < Long.MAX_VALUE is 66. If the computed value exceeds Long.MAX_VALUE an ArithMeticException is thrown.

Parameters
n:int

the size of the set

k:int

the size of the subsets to be counted

Returns:long

n choose k

Annotations
@Deprecated
Exceptions
NotPositiveException:
if n < 0.
NumberIsTooLargeException:
if k > n.
MathArithmeticException:
if the result is too large to be represented by a long integer.
binomialCoefficientDoubleback to summary
public static double binomialCoefficientDouble(final int n, final int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticException

Returns a double representation of the Binomial Coefficient, "n choose k", the number of k-element subsets that can be selected from an n-element set.

Preconditions:

  • 0 <= k <= n (otherwise IllegalArgumentException is thrown)
  • The result is small enough to fit into a double. The largest value of n for which all coefficients are < Double.MAX_VALUE is 1029. If the computed value exceeds Double.MAX_VALUE, Double.POSITIVE_INFINITY is returned

Parameters
n:int

the size of the set

k:int

the size of the subsets to be counted

Returns:double

n choose k

Annotations
@Deprecated
Exceptions
NotPositiveException:
if n < 0.
NumberIsTooLargeException:
if k > n.
MathArithmeticException:
if the result is too large to be represented by a long integer.
binomialCoefficientLogback to summary
public static double binomialCoefficientLog(final int n, final int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticException

Returns the natural log of the Binomial Coefficient, "n choose k", the number of k-element subsets that can be selected from an n-element set.

Preconditions:

  • 0 <= k <= n (otherwise IllegalArgumentException is thrown)

Parameters
n:int

the size of the set

k:int

the size of the subsets to be counted

Returns:double

n choose k

Annotations
@Deprecated
Exceptions
NotPositiveException:
if n < 0.
NumberIsTooLargeException:
if k > n.
MathArithmeticException:
if the result is too large to be represented by a long integer.
factorialback to summary
public static long factorial(final int n) throws NotPositiveException, MathArithmeticException

Returns n!. Shorthand for n Factorial, the product of the numbers 1,...,n.

Preconditions:

  • n >= 0 (otherwise IllegalArgumentException is thrown)
  • The result is small enough to fit into a long. The largest value of n for which n! < Long.MAX_VALUE} is 20. If the computed value exceeds Long.MAX_VALUE an ArithMeticException is thrown.

Parameters
n:int

argument

Returns:long

n!

Annotations
@Deprecated
Exceptions
NotPositiveException:
if n < 0.
MathArithmeticException:
  • if the result is too large to be represented by a long.
  • if n > 20: The factorial value is too large to fit in a long.
factorialDoubleback to summary
public static double factorialDouble(final 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:int

Argument.

Returns:double

n!

Annotations
@Deprecated
Exceptions
NotPositiveException:
if n < 0.
factorialLogback to summary
public static double factorialLog(final int n) throws NotPositiveException

Compute the natural logarithm of the factorial of n.

Parameters
n:int

Argument.

Returns:double

n!

Annotations
@Deprecated
Exceptions
NotPositiveException:
if n < 0.
gcdback to summary
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:int

Number.

q:int

Number.

Returns:int

the greatest common divisor (never negative).

Exceptions
MathArithmeticException:
if the result cannot be represented as a non-negative int value.
Since
1.1
gcdback to summary
public static long gcd(final long p, final 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:long

Number.

q:long

Number.

Returns:long

the greatest common divisor, never negative.

Exceptions
MathArithmeticException:
if the result cannot be represented as a non-negative long value.
Since
2.1
gcdPositiveback to summary
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) and gcd(x, 0) is the value of x.
  • The invocation gcd(0, 0) is the only one which returns 0.
Parameters
a:int

Positive number.

b:int

Positive number.

Returns:int

the greatest common divisor.

isPowerOfTwoback to summary
public static boolean isPowerOfTwo(long n)

Returns true if the argument is a power of two.

Parameters
n:long

the number to test

Returns:boolean

true if the argument is a power of two

lcmback to summary
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:int

Number.

b:int

Number.

Returns:int

the least common multiple, never negative.

Exceptions
MathArithmeticException:
if the result cannot be represented as a non-negative int value.
Since
1.1
lcmback to summary
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:long

Number.

b:long

Number.

Returns:long

the least common multiple, never negative.

Exceptions
MathArithmeticException:
if the result cannot be represented as a non-negative long value.
Since
2.1
mulAndCheckback to summary
public static int mulAndCheck(int x, int y) throws MathArithmeticException

Multiply two integers, checking for overflow.

Parameters
x:int

Factor.

y:int

Factor.

Returns:int

the product x * y.

Exceptions
MathArithmeticException:
if the result can not be represented as an int.
Since
1.1
mulAndCheckback to summary
public static long mulAndCheck(long a, long b) throws MathArithmeticException

Multiply two long integers, checking for overflow.

Parameters
a:long

Factor.

b:long

Factor.

Returns:long

the product a * b.

Exceptions
MathArithmeticException:
if the result can not be represented as a long.
Since
1.2
powback to summary
public static int pow(final int k, final int e) throws NotPositiveException, MathArithmeticException

Raise an int to an int power.

Parameters
k:int

Number to raise.

e:int

Exponent (must be positive or zero).

Returns:int

\( k^e \)

Exceptions
NotPositiveException:
if e < 0.
MathArithmeticException:
if the result would overflow.
powback to summary
public static int pow(final int k, long e) throws NotPositiveException

Deprecated

As of 3.3. Please use pow(int, int) instead.

Raise an int to a long power.

Parameters
k:int

Number to raise.

e:long

Exponent (must be positive or zero).

Returns:int

ke

Annotations
@Deprecated
Exceptions
NotPositiveException:
if e < 0.
powback to summary
public static long pow(final long k, final int e) throws NotPositiveException, MathArithmeticException

Raise a long to an int power.

Parameters
k:long

Number to raise.

e:int

Exponent (must be positive or zero).

Returns:long

\( k^e \)

Exceptions
NotPositiveException:
if e < 0.
MathArithmeticException:
if the result would overflow.
powback to summary
public static long pow(final long k, long e) throws NotPositiveException

Deprecated

As of 3.3. Please use pow(long, int) instead.

Raise a long to a long power.

Parameters
k:long

Number to raise.

e:long

Exponent (must be positive or zero).

Returns:long

ke

Annotations
@Deprecated
Exceptions
NotPositiveException:
if e < 0.
powback to summary
public static BigInteger pow(final BigInteger k, int e) throws NotPositiveException

Raise a BigInteger to an int power.

Parameters
k:BigInteger

Number to raise.

e:int

Exponent (must be positive or zero).

Returns:BigInteger

ke

Exceptions
NotPositiveException:
if e < 0.
powback to summary
public static BigInteger pow(final BigInteger k, long e) throws NotPositiveException

Raise a BigInteger to a long power.

Parameters
k:BigInteger

Number to raise.

e:long

Exponent (must be positive or zero).

Returns:BigInteger

ke

Exceptions
NotPositiveException:
if e < 0.
powback to summary
public static BigInteger pow(final BigInteger k, BigInteger e) throws NotPositiveException

Raise a BigInteger to a BigInteger power.

Parameters
k:BigInteger

Number to raise.

e:BigInteger

Exponent (must be positive or zero).

Returns:BigInteger

ke

Exceptions
NotPositiveException:
if e < 0.
stirlingS2back to summary
public static long stirlingS2(final int n, final int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticException

Returns the Stirling number of the second kind, "S(n,k)", the number of ways of partitioning an n-element set into k non-empty subsets.

The preconditions are 0 <= k <= n (otherwise NotPositiveException is thrown)

Parameters
n:int

the size of the set

k:int

the number of non-empty subsets

Returns:long

S(n,k)

Annotations
@Deprecated
Exceptions
NotPositiveException:
if k < 0.
NumberIsTooLargeException:
if k > 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
subAndCheckback to summary
public static int subAndCheck(int x, int y) throws MathArithmeticException

Subtract two integers, checking for overflow.

Parameters
x:int

Minuend.

y:int

Subtrahend.

Returns:int

the difference x - y.

Exceptions
MathArithmeticException:
if the result can not be represented as an int.
Since
1.1
subAndCheckback to summary
public static long subAndCheck(long a, long b) throws MathArithmeticException

Subtract two long integers, checking for overflow.

Parameters
a:long

Value.

b:long

Value.

Returns:long

the difference a - b.

Exceptions
MathArithmeticException:
if the result can not be represented as a long.
Since
1.2