public final class Numbers extends Object
| Modifier and Type | Field and Description |
|---|---|
static Byte |
BYTE_MINUS_ONE
Reusable Byte constant for minus one.
|
static Byte |
BYTE_ONE
Reusable Byte constant for one.
|
static Byte |
BYTE_ZERO
Reusable Byte constant for zero.
|
static Double |
DOUBLE_MINUS_ONE
Reusable Double constant for minus one.
|
static Double |
DOUBLE_ONE
Reusable Double constant for one.
|
static Double |
DOUBLE_ZERO
Reusable Double constant for zero.
|
static Float |
FLOAT_MINUS_ONE
Reusable Float constant for minus one.
|
static Float |
FLOAT_ONE
Reusable Float constant for one.
|
static Float |
FLOAT_ZERO
Reusable Float constant for zero.
|
static Integer |
INTEGER_MINUS_ONE
Reusable Integer constant for minus one.
|
static Integer |
INTEGER_ONE
Reusable Integer constant for one.
|
static Integer |
INTEGER_TWO
Reusable Integer constant for two
|
static Integer |
INTEGER_ZERO
Reusable Integer constant for zero.
|
static Long |
LONG_MINUS_ONE
Reusable Long constant for minus one.
|
static Long |
LONG_ONE
Reusable Long constant for one.
|
static Long |
LONG_ZERO
Reusable Long constant for zero.
|
static Short |
SHORT_MINUS_ONE
Reusable Short constant for minus one.
|
static Short |
SHORT_ONE
Reusable Short constant for one.
|
static Short |
SHORT_ZERO
Reusable Short constant for zero.
|
| Modifier and Type | Method and Description |
|---|---|
static double |
acosh(double a)
Compute the inverse hyperbolic cosine of a number.
|
static int |
addExact(int a,
int b)
Returns the sum of
a and b, provided it does not overflow. |
static long |
addExact(long a,
long b)
Returns the sum of
a and b, provided it does not overflow. |
static double |
asinh(double a)
Compute the inverse hyperbolic sine of a number.
|
static double |
atanh(double a)
Compute the inverse hyperbolic tangent of a number.
|
static BigInteger |
binnomial(int n,
int k)
Returns
n choose k, also known as the binomial coefficient of n and
k, that is, n! / (k! (n - k)!). |
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 long |
binomiall(int n,
int k)
Returns
n choose k, also known as the binomial coefficient of n and
k, or Long.MAX_VALUE if the result does not fit in a long. |
static int |
castExact(long value)
Returns the
int value that is equal to value, if possible. |
static BigInteger |
ceilingPowerOfTwo(BigInteger x)
Ceiling power of two.
|
static long |
ceilingPowerOfTwo(long x)
Returns the smallest power of two greater than or equal to
x. |
static u.Optional<BigDecimal> |
createBigDecimal(String str)
Convert a
String to a BigDecimal. |
static u.Optional<BigInteger> |
createBigInteger(String str)
Convert a
String to a BigInteger; since 3.2 it
handles hex (0x or #) and octal (0) notations. |
static u.OptionalDouble |
createDouble(String str)
Convert a
String to a Double. |
static u.OptionalFloat |
createFloat(String str)
Convert a
String to a Float. |
static u.OptionalInt |
createInteger(String str)
Convert a
String to a Integer, handling hex
(0xhhhh) and octal (0dddd) notations. |
static u.OptionalLong |
createLong(String str)
Convert a
String to a Long; since 3.1 it
handles hex (0Xhhhh) and octal (0ddd) notations. |
static u.Optional<Number> |
createNumber(String str)
Turns a string value into a java.lang.Number.
|
static BigInteger |
divide(BigInteger p,
BigInteger q,
RoundingMode mode) |
static int |
divide(int p,
int q,
RoundingMode mode)
Returns the result of dividing
p by q, rounding using the specified
RoundingMode. |
static long |
divide(long p,
long q,
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 long |
factoriall(int n)
Returns
n!, that is, the product of the first n positive integers, 1 if
n == 0, or Long.MAX_VALUE if the result does not fit in a long. |
static double |
factorialll(int n)
Returns
n!, that is, the product of the first n positive integers, 1 if
n == 0, or n!, or Double.POSITIVE_INFINITY if
n! > Double.MAX_VALUE. |
static BigInteger |
facttorial(int n)
Returns
n!, that is, the product of the first n positive integers, or 1
if n == 0. |
static BigInteger |
floorPowerOfTwo(BigInteger x)
Floor power of two.
|
static long |
floorPowerOfTwo(long x)
Returns the largest power of two less than or equal to
x. |
static int |
fuzzyCompare(double a,
double b,
double tolerance)
Compares
a and b "fuzzily," with a tolerance for nearly-equal values. |
static boolean |
fuzzyEquals(double a,
double b,
double tolerance)
Returns
true if a and b are within tolerance of each other. |
static int |
gcd(int a,
int b)
Returns the greatest common divisor of
a, b. |
static long |
gcd(long a,
long b)
Returns the greatest common divisor of
a, b. |
static boolean |
isCreatable(String str)
Checks whether the String a valid Java number.
|
static boolean |
isDigits(String str)
Checks whether the
String contains only
digit characters. |
static boolean |
isMathematicalInteger(double x)
Returns
true if x represents a mathematical integer. |
static boolean |
isParsable(String str)
Checks whether the given String is a parsable number.
|
static boolean |
isPerfectSquare(int n)
Checks if is perfect square.
|
static boolean |
isPerfectSquare(long n)
Checks if is perfect square.
|
static boolean |
isPowerOfTwo(BigInteger x)
Returns
true if x represents a power of two. |
static boolean |
isPowerOfTwo(double x)
Checks if is power of two.
|
static boolean |
isPowerOfTwo(int x)
Checks if is power of two.
|
static boolean |
isPowerOfTwo(long x)
Checks if is power of two.
|
static boolean |
isPrime(long n)
Returns
true if n is a
prime number: an integer greater
than one that cannot be factored into a product of smaller positive integers. |
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 double |
log(double a) |
static int |
log10(BigInteger x,
RoundingMode mode)
Returns the base-10 logarithm of
x, rounded according to the specified rounding mode. |
static double |
log10(double x) |
static int |
log10(int x,
RoundingMode mode) |
static int |
log10(long x,
RoundingMode mode)
Returns the base-10 logarithm of
x, rounded according to the specified rounding mode. |
static int |
log2(BigInteger x,
RoundingMode mode)
Returns the base-2 logarithm of
x, rounded according to the specified rounding mode. |
static double |
log2(double x)
Returns the base 2 logarithm of a double value.
|
static int |
log2(double x,
RoundingMode mode)
Returns the base 2 logarithm of a double value, rounded with the specified rounding mode to an
int. |
static int |
log2(int x,
RoundingMode mode) |
static int |
log2(long x,
RoundingMode mode)
Returns the base-2 logarithm of
x, rounded according to the specified rounding mode. |
static double |
mean(double... values) |
static double |
mean(double x,
double y) |
static double |
mean(int... values)
Returns the arithmetic mean of
values. |
static int |
mean(int x,
int y)
Returns the arithmetic mean of
x and y, rounded towards
negative infinity. |
static double |
mean(long... values)
Returns the arithmetic mean of
values. |
static long |
mean(long x,
long y)
Returns the arithmetic mean of
x and y, rounded toward negative infinity. |
static int |
mod(int x,
int m)
Returns
x mod m, a non-negative value less than m. |
static int |
mod(long x,
int m)
Returns
x mod m, a non-negative value less than m. |
static long |
mod(long x,
long m)
Returns
x mod m, a non-negative value less than m. |
static int |
multiplyExact(int a,
int b)
Returns the product of
a and b, provided it does not overflow. |
static long |
multiplyExact(long a,
long b)
Returns the product of
a and b, provided it does not overflow. |
static int |
pow(int b,
int k) |
static long |
pow(long b,
int k)
Returns
b to the kth power. |
static int |
powExact(int b,
int k)
Returns the
b to the kth power, provided it does not overflow. |
static long |
powExact(long b,
int k)
Returns the
b to the kth power, provided it does not overflow. |
static BigInteger |
roundToBigInteger(double x,
RoundingMode mode)
Returns the
BigInteger value that is equal to x rounded with the specified
rounding mode, if possible. |
static int |
roundToInt(double x,
RoundingMode mode)
Returns the
int value that is equal to x rounded with the specified rounding
mode, if possible. |
static long |
roundToLong(double x,
RoundingMode mode)
Returns the
long value that is equal to x rounded with the specified rounding
mode, if possible. |
static int |
saturatedAdd(int a,
int b)
Returns the sum of
a and b unless it would overflow or underflow in which case
Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively. |
static long |
saturatedAdd(long a,
long b)
Returns the sum of
a and b unless it would overflow or underflow in which case
Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively. |
static int |
saturatedCast(long value)
Returns the
int nearest in value to value. |
static int |
saturatedMultiply(int a,
int b)
Returns the product of
a and b unless it would overflow or underflow in which
case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively. |
static long |
saturatedMultiply(long a,
long b)
Returns the product of
a and b unless it would overflow or underflow in which
case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively. |
static int |
saturatedPow(int b,
int k)
Returns the
b to the kth power, unless it would overflow or underflow in which
case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively. |
static long |
saturatedPow(long b,
int k)
Returns the
b to the kth power, unless it would overflow or underflow in which
case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively. |
static int |
saturatedSubtract(int a,
int b)
Returns the difference of
a and b unless it would overflow or underflow in
which case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively. |
static long |
saturatedSubtract(long a,
long b)
Returns the difference of
a and b unless it would overflow or underflow in
which case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively. |
static BigInteger |
sqrt(BigInteger x,
RoundingMode mode) |
static int |
sqrt(int x,
RoundingMode mode)
Returns the square root of
x, rounded with the specified rounding mode. |
static long |
sqrt(long x,
RoundingMode mode)
Returns the square root of
x, rounded with the specified rounding mode. |
static int |
subtractExact(int a,
int b)
Returns the difference of
a and b, provided it does not overflow. |
static long |
subtractExact(long a,
long b)
Returns the difference of
a and b, provided it does not overflow. |
static byte |
toByte(String str) |
static byte |
toByte(String str,
byte defaultValue) |
static double |
toDouble(BigDecimal value)
Convert a
BigDecimal to a double. |
static double |
toDouble(BigDecimal value,
double defaultValue)
Convert a
BigDecimal to a double. |
static double |
toDouble(String str) |
static double |
toDouble(String str,
double defaultValue) |
static float |
toFloat(String str) |
static float |
toFloat(String str,
float defaultValue) |
static int |
toInt(String str) |
static int |
toInt(String str,
int defaultValue) |
static long |
toLong(String str) |
static long |
toLong(String str,
long defaultValue) |
static short |
toShort(String str) |
static short |
toShort(String str,
short defaultValue) |
public static final Byte BYTE_ZERO
public static final Byte BYTE_ONE
public static final Byte BYTE_MINUS_ONE
public static final Short SHORT_ZERO
public static final Short SHORT_ONE
public static final Short SHORT_MINUS_ONE
public static final Integer INTEGER_ZERO
public static final Integer INTEGER_ONE
public static final Integer INTEGER_TWO
public static final Integer INTEGER_MINUS_ONE
public static final Long LONG_ZERO
public static final Long LONG_ONE
public static final Long LONG_MINUS_ONE
public static final Float FLOAT_ZERO
public static final Float FLOAT_ONE
public static final Float FLOAT_MINUS_ONE
public static final Double DOUBLE_ZERO
public static final Double DOUBLE_ONE
public static final Double DOUBLE_MINUS_ONE
public static byte toByte(String str) throws NumberFormatException
str - 0 if the specified str is null or empty.NumberFormatException - If the string is not a parsable byte.isParsable(String),
Byte.parseByte(String),
Byte.decode(String)public static byte toByte(String str, byte defaultValue) throws NumberFormatException
str - defaultValue - defaultValue if the specified str is null or empty.NumberFormatException - If the string is not a parsable byte.isParsable(String),
Byte.parseByte(String),
Byte.decode(String)public static short toShort(String str) throws NumberFormatException
str - 0 if the specified str is null or empty.NumberFormatException - If the string is not a parsable short.isParsable(String),
Short.parseShort(String),
Short.decode(String)public static short toShort(String str, short defaultValue) throws NumberFormatException
str - defaultValue - defaultValue if the specified str is null or empty.NumberFormatException - If the string is not a parsable short.isParsable(String),
Short.parseShort(String),
Short.decode(String)public static int toInt(String str) throws NumberFormatException
str - 0 if the specified str is null or empty.NumberFormatException - If the string is not a parsable int.isParsable(String),
Integer.parseInt(String),
Integer.decode(String)public static int toInt(String str, int defaultValue) throws NumberFormatException
str - defaultValue - defaultValue if the specified str is null or empty.NumberFormatException - If the string is not a parsable int.isParsable(String),
Integer.parseInt(String),
Integer.decode(String)public static long toLong(String str) throws NumberFormatException
str - 0 if the specified str is null or empty.NumberFormatException - If the string is not a parsable long.isParsable(String),
Long.parseLong(String),
Long.decode(String)public static long toLong(String str, long defaultValue) throws NumberFormatException
str - defaultValue - defaultValue if the specified str is null or empty.NumberFormatException - If the string is not a parsable long.isParsable(String),
Long.parseLong(String),
Long.decode(String)public static float toFloat(String str) throws NumberFormatException
str - 0 if the specified str is null or empty.NumberFormatException - If the string is not a parsable float.isParsable(String),
Float.parseFloat(String)public static float toFloat(String str, float defaultValue) throws NumberFormatException
str - defaultValue - defaultValue if the specified str is null or empty.NumberFormatException - If the string is not a parsable float.isParsable(String),
Float.parseFloat(String)public static double toDouble(String str) throws NumberFormatException
str - 0.0d if the specified str is null or empty.NumberFormatException - If the string is not a parsable double.isParsable(String),
Double.parseDouble(String)public static double toDouble(String str, double defaultValue) throws NumberFormatException
str - defaultValue - defaultValue if the specified str is null or empty.NumberFormatException - If the string is not a parsable double.isParsable(String),
Double.parseDouble(String)public static double toDouble(BigDecimal value)
Convert a BigDecimal to a double.
If the BigDecimal value is
null, then the specified default value is returned.
NumberUtils.toDouble(null) = 0.0d NumberUtils.toDouble(BigDecimal.valudOf(8.5d)) = 8.5d
value - the BigDecimal to convert, may be null.BigDecimal or
0.0d if the BigDecimal is null.public static double toDouble(BigDecimal value, double defaultValue)
Convert a BigDecimal to a double.
If the BigDecimal value is
null, then the specified default value is returned.
NumberUtils.toDouble(null, 1.1d) = 1.1d NumberUtils.toDouble(BigDecimal.valudOf(8.5d), 1.1d) = 8.5d
value - the BigDecimal to convert, may be null.defaultValue - the default valueBigDecimal or the
defaultValue if the BigDecimal is null.public static u.OptionalInt createInteger(String str)
Convert a String to a Integer, handling hex
(0xhhhh) and octal (0dddd) notations. N.B. a leading zero means octal;
spaces are not trimmed.
Returns an empty OptionalInt if the string is null or can't be parsed as Integer.
str - a String to convert, may be nullisCreatable(String)public static u.OptionalLong createLong(String str)
Convert a String to a Long; since 3.1 it
handles hex (0Xhhhh) and octal (0ddd) notations. N.B. a leading zero
means octal; spaces are not trimmed.
Returns an empty OptionalLong if the string is null or can't be parsed as Long.
str - a String to convert, may be nullisCreatable(String)public static u.OptionalFloat createFloat(String str)
Convert a String to a Float.
Returns an empty OptionalFloat if the string is null or can't be parsed as Float.
str - a String to convert, may be nullisCreatable(String)public static u.OptionalDouble createDouble(String str)
Convert a String to a Double.
Returns an empty OptionalDouble if the string is null or can't be parsed as Double.
str - a String to convert, may be nullisCreatable(String)public static u.Optional<BigInteger> createBigInteger(String str)
Convert a String to a BigInteger; since 3.2 it
handles hex (0x or #) and octal (0) notations.
Returns an empty Optional if the string is null or can't be parsed as BigInteger.
str - a String to convert, may be nullisCreatable(String)public static u.Optional<BigDecimal> createBigDecimal(String str)
Convert a String to a BigDecimal.
Returns an empty Optional if the string is null or can't be parsed as BigDecimal.
str - a String to convert, may be nullisCreatable(String)public static u.Optional<Number> createNumber(String str)
Turns a string value into a java.lang.Number.
If the string starts with 0x or -0x (lower or upper case)
or # or -#, it will be interpreted as a hexadecimal
Integer - or Long, if the number of digits after the prefix is more than
8 - or BigInteger if there are more than 16 digits.
Then, the value is examined for a type qualifier on the end, i.e. one of
'f','F','d','D','l','L'. If it is found, it starts trying to
create successively larger types from the type specified until one is
found that can represent the value.
If a type specifier is not found, it will check for a decimal point and
then try successively larger types from Integer to
BigInteger and from double to
BigDecimal.
Integral values with a leading 0 will be interpreted as octal;
the returned number will be Integer, Long or BigDecimal as appropriate.
Returns an empty Optional if the string is null or can't be parsed as Number.
str - a String containing a number, may be nullisCreatable(String)public static boolean isCreatable(String str)
Checks whether the String a valid Java number.
Valid numbers include hexadecimal marked with the 0x or
0X qualifier, octal numbers, scientific notation and
numbers marked with a type qualifier (e.g. 123L).
Non-hexadecimal strings beginning with a leading zero are
treated as octal values. Thus the string 09 will return
false, since 9 is not a valid octal value.
However, numbers beginning with 0. are treated as decimal.
null and empty/blank String will return
false.
Note, createNumber(String) should return a number for every
input resulting in true.
str - the String to checktrue if the string is a correctly formatted numberpublic static boolean isParsable(String str)
Checks whether the given String is a parsable number.
Parsable numbers include those Strings understood by Integer.parseInt(String),
Long.parseLong(String), Float.parseFloat(String) or
Double.parseDouble(String). This method can be used instead of catching NumberFormatException
when calling one of those methods.
Hexadecimal and scientific notations are not considered parsable.
See isCreatable(String) on those cases.
Null and empty String will return false.
str - the String to check.true if the string is a parsable number.public static boolean isDigits(String str)
Checks whether the String contains only
digit characters.
Null and empty String will return
false.
str - the String to checktrue if str contains only Unicode numericpublic static boolean isPrime(long n)
true if n is a
prime number: an integer greater
than one that cannot be factored into a product of smaller positive integers.
Returns false if n is zero, one, or a composite number (one which can
be factored into smaller positive integers).
To test larger numbers, use BigInteger.isProbablePrime(int).
n - IllegalArgumentException - if n is negativepublic static boolean isPerfectSquare(int n)
n - public static boolean isPerfectSquare(long n)
n - public static boolean isPowerOfTwo(int x)
x - public static boolean isPowerOfTwo(long x)
x - public static boolean isPowerOfTwo(double x)
x - public static boolean isPowerOfTwo(BigInteger x)
true if x represents a power of two.x - public static double log(double a)
a - public static int log2(int x,
RoundingMode mode)
x - mode - public static int log2(long x,
RoundingMode mode)
x, rounded according to the specified rounding mode.x - mode - IllegalArgumentException - if x <= 0ArithmeticException - if mode is RoundingMode.UNNECESSARY and x
is not a power of twopublic static double log2(double x)
Special cases:
x is NaN or less than zero, the result is NaN.
x is positive infinity, the result is positive infinity.
x is positive or negative zero, the result is negative infinity.
The computed result is within 1 ulp of the exact result.
If the result of this method will be immediately rounded to an int,
log2(double, RoundingMode) is faster.
x - public static int log2(double x,
RoundingMode mode)
int.
Regardless of the rounding mode, this is faster than (int) log2(x).
x - mode - IllegalArgumentException - if x <= 0.0, x is NaN, or x is
infinitepublic static int log2(BigInteger x, RoundingMode mode)
x, rounded according to the specified rounding mode.x - mode - IllegalArgumentException - if x <= 0ArithmeticException - if mode is RoundingMode.UNNECESSARY and x
is not a power of twopublic static int log10(int x,
RoundingMode mode)
x - mode - public static int log10(long x,
RoundingMode mode)
x, rounded according to the specified rounding mode.x - mode - IllegalArgumentException - if x <= 0ArithmeticException - if mode is RoundingMode.UNNECESSARY and x
is not a power of tenpublic static double log10(double x)
x - public static int log10(BigInteger x, RoundingMode mode)
x, rounded according to the specified rounding mode.x - mode - IllegalArgumentException - if x <= 0ArithmeticException - if mode is RoundingMode.UNNECESSARY and x
is not a power of tenpublic static int pow(int b,
int k)
b - k - public static long pow(long b,
int k)
b to the kth power. Even if the result overflows, it will be equal to
BigInteger.valueOf(b).pow(k).longValue(). This implementation runs in O(log k)
time.b - k - IllegalArgumentException - if k < 0public static long ceilingPowerOfTwo(long x)
x. This is equivalent to
checkedPow(2, log2(x, CEILING)).x - IllegalArgumentException - if x <= 0ArithmeticException - of the next-higher power of two is not representable as a
long, i.e. when x > 2^62public static BigInteger ceilingPowerOfTwo(BigInteger x)
x - public static long floorPowerOfTwo(long x)
x. This is equivalent to
checkedPow(2, log2(x, FLOOR)).x - IllegalArgumentException - if x <= 0public static BigInteger floorPowerOfTwo(BigInteger x)
x - public static int sqrt(int x,
RoundingMode mode)
x, rounded with the specified rounding mode.x - mode - IllegalArgumentException - if x < 0ArithmeticException - if mode is RoundingMode.UNNECESSARY and
sqrt(x) is not an integerpublic static long sqrt(long x,
RoundingMode mode)
x, rounded with the specified rounding mode.x - mode - IllegalArgumentException - if x < 0ArithmeticException - if mode is RoundingMode.UNNECESSARY and
sqrt(x) is not an integerpublic static BigInteger sqrt(BigInteger x, RoundingMode mode)
x - mode - public static int divide(int p,
int q,
RoundingMode mode)
p by q, rounding using the specified
RoundingMode.p - q - mode - ArithmeticException - if q == 0, or if mode == UNNECESSARY and a
is not an integer multiple of bpublic static long divide(long p,
long q,
RoundingMode mode)
p by q, rounding using the specified
RoundingMode.p - q - mode - ArithmeticException - if q == 0, or if mode == UNNECESSARY and a
is not an integer multiple of bpublic static BigInteger divide(BigInteger p, BigInteger q, RoundingMode mode)
p - q - mode - public 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 - m - ArithmeticException - if m <= 0public static int mod(long 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 - m - ArithmeticException - if m <= 0public static long mod(long x,
long 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 - m - ArithmeticException - if m <= 0public static int gcd(int a,
int b)
a, b. Returns 0 if
a == 0 && b == 0.a - b - IllegalArgumentException - if a < 0 or b < 0public static long gcd(long a,
long b)
a, b. Returns 0 if
a == 0 && b == 0.a - b - IllegalArgumentException - if a < 0 or b < 0public static int lcm(int a,
int b)
throws ArithmeticException
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)
throws ArithmeticException
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 addExact(int a,
int b)
a and b, provided it does not overflow.a - b - ArithmeticException - if a + b overflows in signed int arithmeticpublic static long addExact(long a,
long b)
a and b, provided it does not overflow.a - b - ArithmeticException - if a + b overflows in signed long arithmeticpublic static int subtractExact(int a,
int b)
a and b, provided it does not overflow.a - b - ArithmeticException - if a - b overflows in signed int arithmeticpublic static long subtractExact(long a,
long b)
a and b, provided it does not overflow.a - b - ArithmeticException - if a - b overflows in signed long arithmeticpublic static int multiplyExact(int a,
int b)
a and b, provided it does not overflow.a - b - ArithmeticException - if a * b overflows in signed int arithmeticpublic static long multiplyExact(long a,
long b)
a and b, provided it does not overflow.a - b - ArithmeticException - if a * b overflows in signed long arithmeticpublic static int powExact(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 - k - ArithmeticException - if b to the kth power overflows in signed
int arithmeticpublic static long powExact(long b,
int k)
b to the kth power, provided it does not overflow.b - k - ArithmeticException - if b to the kth power overflows in signed
long arithmeticpublic static int castExact(long value)
int value that is equal to value, if possible.value - any value in the range of the int typeIllegalArgumentException - if value is greater than Integer.MAX_VALUE or
less than Integer.MIN_VALUEpublic static int saturatedAdd(int a,
int b)
a and b unless it would overflow or underflow in which case
Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively.a - b - public static long saturatedAdd(long a,
long b)
a and b unless it would overflow or underflow in which case
Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively.a - b - public static int saturatedSubtract(int a,
int b)
a and b unless it would overflow or underflow in
which case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively.a - b - public static long saturatedSubtract(long a,
long b)
a and b unless it would overflow or underflow in
which case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively.a - b - public static int saturatedMultiply(int a,
int b)
a and b unless it would overflow or underflow in which
case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively.a - b - public static long saturatedMultiply(long a,
long b)
a and b unless it would overflow or underflow in which
case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively.a - b - public static int saturatedPow(int b,
int k)
b to the kth power, unless it would overflow or underflow in which
case Integer.MAX_VALUE or Integer.MIN_VALUE is returned, respectively.b - k - public static long saturatedPow(long b,
int k)
b to the kth power, unless it would overflow or underflow in which
case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively.b - k - public static int saturatedCast(long value)
int nearest in value to value.value - any long valueInteger.MAX_VALUE if it is too large, or Integer.MIN_VALUE if it is too
smallpublic 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 - IllegalArgumentException - if n < 0public static long factoriall(int n)
n!, that is, the product of the first n positive integers, 1 if
n == 0, or Long.MAX_VALUE if the result does not fit in a long.n - IllegalArgumentException - if n < 0public static double factorialll(int n)
n!, that is, the product of the first n positive integers, 1 if
n == 0, or n!, or Double.POSITIVE_INFINITY if
n! > Double.MAX_VALUE.
The result is within 1 ulp of the true value.
n - IllegalArgumentException - if n < 0public static BigInteger facttorial(int n)
n!, that is, the product of the first n positive integers, or 1
if n == 0.
Warning: the result takes O(n log n) space, so use cautiously.
This uses an efficient binary recursive algorithm to compute the factorial with balanced multiplies. It also removes all the 2s from the intermediate products (shifting them back in at the end).
n - 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 - k - IllegalArgumentException - if n < 0, k < 0 or k > npublic static long binomiall(int n,
int k)
n choose k, also known as the binomial coefficient of n and
k, or Long.MAX_VALUE if the result does not fit in a long.n - k - IllegalArgumentException - if n < 0, k < 0, or k > npublic static BigInteger binnomial(int n, int k)
n choose k, also known as the binomial coefficient of n and
k, that is, n! / (k! (n - k)!).
Warning: the result can take as much as O(k log n) space.
n - k - 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 - y - public static long mean(long x,
long y)
x and y, rounded toward negative infinity. This
method is resilient to overflow.x - y - public static double mean(double x,
double y)
x - y - @SafeVarargs public static double mean(int... values)
values.
If these values are a sample drawn from a population, this is also an unbiased estimator of the arithmetic mean of the population.
values - a nonempty series of valuesIllegalArgumentException - if values is empty@SafeVarargs public static double mean(long... values)
values.
If these values are a sample drawn from a population, this is also an unbiased estimator of the arithmetic mean of the population.
values - a nonempty series of values, which will be converted to double values
(this may cause loss of precision for longs of magnitude over 2^53 (slightly over 9e15))IllegalArgumentException - if values is empty@SafeVarargs public static double mean(double... values)
values - public static int roundToInt(double x,
RoundingMode mode)
int value that is equal to x rounded with the specified rounding
mode, if possible.x - mode - ArithmeticException - if
x is infinite or NaN
x, after being rounded to a mathematical integer using the specified rounding
mode, is either less than Integer.MIN_VALUE or greater than Integer.MAX_VALUE
x is not a mathematical integer and mode is
RoundingMode.UNNECESSARY
public static long roundToLong(double x,
RoundingMode mode)
long value that is equal to x rounded with the specified rounding
mode, if possible.x - mode - ArithmeticException - if
x is infinite or NaN
x, after being rounded to a mathematical integer using the specified rounding
mode, is either less than Long.MIN_VALUE or greater than Long.MAX_VALUE
x is not a mathematical integer and mode is
RoundingMode.UNNECESSARY
public static BigInteger roundToBigInteger(double x, RoundingMode mode)
BigInteger value that is equal to x rounded with the specified
rounding mode, if possible.x - mode - ArithmeticException - if
x is infinite or NaN
x is not a mathematical integer and mode is
RoundingMode.UNNECESSARY
public static boolean fuzzyEquals(double a,
double b,
double tolerance)
true if a and b are within tolerance of each other.
Technically speaking, this is equivalent to
Math.abs(a - b) <= tolerance || Double.valueOf(a).equals(Double.valueOf(b)).
Notable special cases include:
a == b, then a and b are always fuzzily equal.
tolerance is zero, and neither a nor b is NaN, then a
and b are fuzzily equal if and only if a == b.
Double.POSITIVE_INFINITY tolerance, all non-NaN values are fuzzily equal.
Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY are fuzzily equal only to themselves.
This is reflexive and symmetric, but not transitive, so it is not an
equivalence relation and not suitable for use in Object.equals(java.lang.Object)
implementations.
a - b - tolerance - IllegalArgumentException - if tolerance is < 0 or NaNpublic static int fuzzyCompare(double a,
double b,
double tolerance)
a and b "fuzzily," with a tolerance for nearly-equal values.
This method is equivalent to
fuzzyEquals(a, b, tolerance) ? 0 : Double.compare(a, b). In particular, like
Double.compare(double, double), it treats all NaN values as equal and greater than all
other values (including Double.POSITIVE_INFINITY).
This is not a total ordering and is not suitable for use in
Comparable.compareTo(T) implementations. In particular, it is not transitive.
a - b - tolerance - IllegalArgumentException - if tolerance is < 0 or NaNpublic static boolean isMathematicalInteger(double x)
true if x represents a mathematical integer.
This is equivalent to, but not necessarily implemented as, the expression !Double.isNaN(x) && !Double.isInfinite(x) && x == Math.rint(x).
x - public static double asinh(double a)
a - number on which evaluation is donepublic static double acosh(double a)
a - number on which evaluation is donepublic static double atanh(double a)
a - number on which evaluation is doneCopyright © 2021. All rights reserved.