Class ArithmeticUtils
- java.lang.Object
-
- org.hipparchus.util.ArithmeticUtils
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
addAndCheck(int x, int y)
Add two integers, checking for overflow.static long
addAndCheck(long a, long b)
Add two long integers, checking for overflow.static int
divideUnsigned(int dividend, int divisor)
Returns the unsigned quotient of dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.static long
divideUnsigned(long dividend, long divisor)
Returns the unsigned quotient of dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.static int
gcd(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 long
gcd(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.static boolean
isPowerOfTwo(long n)
Returns true if the argument is a power of two.static int
lcm(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 long
lcm(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 int
mulAndCheck(int x, int y)
Multiply two integers, checking for overflow.static long
mulAndCheck(long a, long b)
Multiply two long integers, checking for overflow.static int
pow(int k, int e)
Raise an int to an int power.static long
pow(long k, int e)
Raise a long to an int power.static BigInteger
pow(BigInteger k, int e)
Raise a BigInteger to an int power.static BigInteger
pow(BigInteger k, long e)
Raise a BigInteger to a long power.static BigInteger
pow(BigInteger k, BigInteger e)
Raise a BigInteger to a BigInteger power.static int
remainderUnsigned(int dividend, int divisor)
Returns the unsigned remainder from dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.static long
remainderUnsigned(long dividend, long divisor)
Returns the unsigned remainder from dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.static int
subAndCheck(int x, int y)
Subtract two integers, checking for overflow.static long
subAndCheck(long a, long b)
Subtract two long integers, checking for overflow.
-
-
-
Method Detail
-
addAndCheck
public static int addAndCheck(int x, int y) throws MathRuntimeException
Add two integers, checking for overflow.- Parameters:
x
- an addendy
- an addend- Returns:
- the sum
x+y
- Throws:
MathRuntimeException
- if the result can not be represented as anint
.
-
addAndCheck
public static long addAndCheck(long a, long b) throws MathRuntimeException
Add two long integers, checking for overflow.- Parameters:
a
- an addendb
- an addend- Returns:
- the sum
a+b
- Throws:
MathRuntimeException
- if the result can not be represented as an long
-
gcd
public static int gcd(int p, int q) throws MathRuntimeException
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)
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:
MathRuntimeException
- if the result cannot be represented as a non-negativeint
value.
- The invocations
-
gcd
public static long gcd(long p, long q) throws MathRuntimeException
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)
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:
MathRuntimeException
- if the result cannot be represented as a non-negativelong
value.
- The invocations
-
lcm
public static int lcm(int a, int b) throws MathRuntimeException
Returns the least common multiple of the absolute value of two numbers, using the formulalcm(a,b) = (a / gcd(a,b)) * b
.Special cases:
- 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)
is0
for anyx
.
- Parameters:
a
- Number.b
- Number.- Returns:
- the least common multiple, never negative.
- Throws:
MathRuntimeException
- if the result cannot be represented as a non-negativeint
value.
- The invocations
-
lcm
public static long lcm(long a, long b) throws MathRuntimeException
Returns the least common multiple of the absolute value of two numbers, using the formulalcm(a,b) = (a / gcd(a,b)) * b
.Special cases:
- 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)
is0L
for anyx
.
- Parameters:
a
- Number.b
- Number.- Returns:
- the least common multiple, never negative.
- Throws:
MathRuntimeException
- if the result cannot be represented as a non-negativelong
value.
- The invocations
-
mulAndCheck
public static int mulAndCheck(int x, int y) throws MathRuntimeException
Multiply two integers, checking for overflow.- Parameters:
x
- Factor.y
- Factor.- Returns:
- the product
x * y
. - Throws:
MathRuntimeException
- if the result can not be represented as anint
.
-
mulAndCheck
public static long mulAndCheck(long a, long b) throws MathRuntimeException
Multiply two long integers, checking for overflow.- Parameters:
a
- Factor.b
- Factor.- Returns:
- the product
a * b
. - Throws:
MathRuntimeException
- if the result can not be represented as along
.
-
subAndCheck
public static int subAndCheck(int x, int y) throws MathRuntimeException
Subtract two integers, checking for overflow.- Parameters:
x
- Minuend.y
- Subtrahend.- Returns:
- the difference
x - y
. - Throws:
MathRuntimeException
- if the result can not be represented as anint
.
-
subAndCheck
public static long subAndCheck(long a, long b) throws MathRuntimeException
Subtract two long integers, checking for overflow.- Parameters:
a
- Value.b
- Value.- Returns:
- the difference
a - b
. - Throws:
MathRuntimeException
- if the result can not be represented as along
.
-
pow
public static int pow(int k, int e) throws MathIllegalArgumentException, MathRuntimeException
Raise an int to an int power.- Parameters:
k
- Number to raise.e
- Exponent (must be positive or zero).- Returns:
- \( k^e \)
- Throws:
MathIllegalArgumentException
- ife < 0
.MathRuntimeException
- if the result would overflow.
-
pow
public static long pow(long k, int e) throws MathIllegalArgumentException, MathRuntimeException
Raise a long to an int power.- Parameters:
k
- Number to raise.e
- Exponent (must be positive or zero).- Returns:
- \( k^e \)
- Throws:
MathIllegalArgumentException
- ife < 0
.MathRuntimeException
- if the result would overflow.
-
pow
public static BigInteger pow(BigInteger k, int e) throws MathIllegalArgumentException
Raise a BigInteger to an int power.- Parameters:
k
- Number to raise.e
- Exponent (must be positive or zero).- Returns:
- ke
- Throws:
MathIllegalArgumentException
- ife < 0
.
-
pow
public static BigInteger pow(BigInteger k, long e) throws MathIllegalArgumentException
Raise a BigInteger to a long power.- Parameters:
k
- Number to raise.e
- Exponent (must be positive or zero).- Returns:
- ke
- Throws:
MathIllegalArgumentException
- ife < 0
.
-
pow
public static BigInteger pow(BigInteger k, BigInteger e) throws MathIllegalArgumentException
Raise a BigInteger to a BigInteger power.- Parameters:
k
- Number to raise.e
- Exponent (must be positive or zero).- Returns:
- ke
- Throws:
MathIllegalArgumentException
- ife < 0
.
-
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
-
remainderUnsigned
public static int remainderUnsigned(int dividend, int divisor)
Returns the unsigned remainder from dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.This method does not use the
long
datatype.- Parameters:
dividend
- the value to be divideddivisor
- the value doing the dividing- Returns:
- the unsigned remainder of the first argument divided by the second argument.
-
remainderUnsigned
public static long remainderUnsigned(long dividend, long divisor)
Returns the unsigned remainder from dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.This method does not use the
BigInteger
datatype.- Parameters:
dividend
- the value to be divideddivisor
- the value doing the dividing- Returns:
- the unsigned remainder of the first argument divided by the second argument.
-
divideUnsigned
public static int divideUnsigned(int dividend, int divisor)
Returns the unsigned quotient of dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.Note that in two's complement arithmetic, the three other basic arithmetic operations of add, subtract, and multiply are bit-wise identical if the two operands are regarded as both being signed or both being unsigned. Therefore separate
addUnsigned
, etc. methods are not provided.This method does not use the
long
datatype.- Parameters:
dividend
- the value to be divideddivisor
- the value doing the dividing- Returns:
- the unsigned quotient of the first argument divided by the second argument
-
divideUnsigned
public static long divideUnsigned(long dividend, long divisor)
Returns the unsigned quotient of dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.Note that in two's complement arithmetic, the three other basic arithmetic operations of add, subtract, and multiply are bit-wise identical if the two operands are regarded as both being signed or both being unsigned. Therefore separate
addUnsigned
, etc. methods are not provided.This method does not use the
BigInteger
datatype.- Parameters:
dividend
- the value to be divideddivisor
- the value doing the dividing- Returns:
- the unsigned quotient of the first argument divided by the second argument.
-
-