public final class IntMath
extends java.lang.Object
| Modifier and Type | Method and Description |
|---|---|
static int |
binomial(int n,
int k)
Returns
n choose k, also known as the binomial coefficient of n and
k, or Integer.MAX_VALUE if the result does not fit in an int. |
static int |
checkedAdd(int a,
int b)
Returns the sum of
a and b, provided it does not overflow. |
static int |
checkedMultiply(int a,
int b)
Returns the product of
a and b, provided it does not overflow. |
static int |
checkedPow(int b,
int k)
Returns the
b to the kth power, provided it does not overflow. |
static int |
checkedSubtract(int a,
int b)
Returns the difference of
a and b, provided it does not overflow. |
static int |
divide(int p,
int q,
java.math.RoundingMode mode)
Returns the result of dividing
p by q, rounding using the specified
RoundingMode. |
static int |
factorial(int n)
Returns
n!, that is, the product of the first n positive
integers, 1 if n == 0, or Integer.MAX_VALUE if the
result does not fit in a int. |
static int |
gcd(int a,
int b)
Returns the greatest common divisor of
a, b. |
static boolean |
isPowerOfTwo(int x)
Returns
true if x represents a power of two. |
static int |
log10(int x,
java.math.RoundingMode mode)
Returns the base-10 logarithm of
x, rounded according to the specified rounding mode. |
static int |
log2(int x,
java.math.RoundingMode mode)
Returns the base-2 logarithm of
x, rounded according to the specified rounding mode. |
static int |
mean(int x,
int y)
Returns the arithmetic mean of
x and y, rounded towards
negative infinity. |
static int |
mod(int x,
int m)
Returns
x mod m, a non-negative value less than m. |
static int |
pow(int b,
int k)
Returns
b to the kth power. |
static int |
sqrt(int x,
java.math.RoundingMode mode)
Returns the square root of
x, rounded with the specified rounding mode. |
public static boolean isPowerOfTwo(int x)
true if x represents a power of two.
This differs from Integer.bitCount(x) == 1, because
Integer.bitCount(Integer.MIN_VALUE) == 1, but Integer.MIN_VALUE is not a power
of two.
x - xpublic static int log2(int x,
java.math.RoundingMode mode)
x, rounded according to the specified rounding mode.x - xmode - modejava.lang.IllegalArgumentException - if x <= 0java.lang.ArithmeticException - if mode is RoundingMode.UNNECESSARY and x
is not a power of twopublic static int log10(int x,
java.math.RoundingMode mode)
x, rounded according to the specified rounding mode.x - xmode - modejava.lang.IllegalArgumentException - if x <= 0java.lang.ArithmeticException - if mode is RoundingMode.UNNECESSARY and x
is not a power of tenpublic static int pow(int b,
int k)
b to the kth power. Even if the result overflows, it will be equal to
BigInteger.valueOf(b).pow(k).intValue(). This implementation runs in O(log k)
time.
Compare checkedPow(int, int), which throws an ArithmeticException upon overflow.
b - bk - kjava.lang.IllegalArgumentException - if k < 0public static int sqrt(int x,
java.math.RoundingMode mode)
x, rounded with the specified rounding mode.x - xmode - modejava.lang.IllegalArgumentException - if x < 0java.lang.ArithmeticException - if mode is RoundingMode.UNNECESSARY and
sqrt(x) is not an integerpublic static int divide(int p,
int q,
java.math.RoundingMode mode)
p by q, rounding using the specified
RoundingMode.p - pq - qmode - modejava.lang.ArithmeticException - if q == 0, or if mode == UNNECESSARY and a
is not an integer multiple of bpublic static int mod(int x,
int m)
x mod m, a non-negative value less than m.
This differs from x % m, which might be negative.
For example:
mod(7, 4) == 3
mod(-7, 4) == 1
mod(-1, 4) == 3
mod(-8, 4) == 0
mod(8, 4) == 0x - xm - mjava.lang.ArithmeticException - if m <= 0public static int gcd(int a,
int b)
a, b. Returns 0 if
a == 0 && b == 0.a - ab - bjava.lang.IllegalArgumentException - if a < 0 or b < 0public static int checkedAdd(int a,
int b)
a and b, provided it does not overflow.a - ab - bjava.lang.ArithmeticException - if a + b overflows in signed int arithmeticpublic static int checkedSubtract(int a,
int b)
a and b, provided it does not overflow.a - ab - bjava.lang.ArithmeticException - if a - b overflows in signed int arithmeticpublic static int checkedMultiply(int a,
int b)
a and b, provided it does not overflow.a - ab - bjava.lang.ArithmeticException - if a * b overflows in signed int arithmeticpublic static int checkedPow(int b,
int k)
b to the kth power, provided it does not overflow.
pow(int, int) may be faster, but does not check for overflow.
b - bk - kjava.lang.ArithmeticException - if b to the kth power overflows in signed
int arithmeticpublic static int factorial(int n)
n!, that is, the product of the first n positive
integers, 1 if n == 0, or Integer.MAX_VALUE if the
result does not fit in a int.n - njava.lang.IllegalArgumentException - if n < 0public static int binomial(int n,
int k)
n choose k, also known as the binomial coefficient of n and
k, or Integer.MAX_VALUE if the result does not fit in an int.n - nk - kjava.lang.IllegalArgumentException - if n < 0, k < 0 or k > npublic static int mean(int x,
int y)
x and y, rounded towards
negative infinity. This method is overflow resilient.x - xy - y