public final class ArithmeticUtils extends Object
Math
.Modifier and Type | Method and Description |
---|---|
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 formula
lcm(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 formula
lcm(a,b) = (a / gcd(a,b)) * b . |
static BigInteger |
pow(BigInteger k,
BigInteger e)
Raise a BigInteger to a BigInteger 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 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 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.
|
public static int gcd(int p, int q)
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.gcd(x, x)
, gcd(0, x)
and
gcd(x, 0)
is the absolute value of x
, except
for the special cases above.gcd(0, 0)
is the only one which returns
0
.p
- Number.q
- Number.ArithmeticException
- if the result cannot be represented as
a non-negative int
value.public 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. See Knuth 4.5.2 algorithm B. This algorithm is due to Josef Stein (1961).
Special cases: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.gcd(x, x)
, gcd(0L, x)
and
gcd(x, 0L)
is the absolute value of x
, except
for the special cases above.
gcd(0L, 0L)
is the only one which returns
0L
.p
- Number.q
- Number.ArithmeticException
- if the result cannot be represented as
a non-negative long
value.public static int lcm(int a, int b)
Returns the least common multiple of the absolute value of two numbers,
using the formula lcm(a,b) = (a / gcd(a,b)) * b
.
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.lcm(0, x)
and lcm(x, 0)
is
0
for any x
.
a
- Number.b
- Number.ArithmeticException
- if the result cannot be represented as
a non-negative int
value.public static long lcm(long a, long b)
Returns the least common multiple of the absolute value of two numbers,
using the formula lcm(a,b) = (a / gcd(a,b)) * b
.
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.lcm(0L, x)
and lcm(x, 0L)
is
0L
for any x
.
a
- Number.b
- Number.ArithmeticException
- if the result cannot be represented
as a non-negative long
value.public static int pow(int k, int e)
k
- Number to raise.e
- Exponent (must be positive or zero).IllegalArgumentException
- if e < 0
.ArithmeticException
- if the result would overflow.public static long pow(long k, int e)
k
- Number to raise.e
- Exponent (must be positive or zero).IllegalArgumentException
- if e < 0
.ArithmeticException
- if the result would overflow.public static BigInteger pow(BigInteger k, int e)
k
- Number to raise.e
- Exponent (must be positive or zero).IllegalArgumentException
- if e < 0
.public static BigInteger pow(BigInteger k, long e)
k
- Number to raise.e
- Exponent (must be positive or zero).IllegalArgumentException
- if e < 0
.public static BigInteger pow(BigInteger k, BigInteger e)
k
- Number to raise.e
- Exponent (must be positive or zero).IllegalArgumentException
- if e < 0
.public static boolean isPowerOfTwo(long n)
n
- the number to testpublic static int remainderUnsigned(int dividend, int divisor)
This method does not use the long
datatype.
dividend
- the value to be divideddivisor
- the value doing the dividingpublic static long remainderUnsigned(long dividend, long divisor)
This method does not use the BigInteger
datatype.
dividend
- the value to be divideddivisor
- the value doing the dividingpublic static int divideUnsigned(int dividend, int divisor)
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.
dividend
- the value to be divideddivisor
- the value doing the dividingpublic static long divideUnsigned(long dividend, long divisor)
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.
dividend
- the value to be divideddivisor
- the value doing the dividingCopyright © 2017–2020 The Apache Software Foundation. All rights reserved.