Class Numbers
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Byte
Reusable Byte constant for minus one.static final Byte
Reusable Byte constant for one.static final Byte
Reusable Byte constant for zero.static final Double
Reusable Double constant for minus one.static final Double
Reusable Double constant for one.static final Double
Reusable Double constant for zero.static final Float
Reusable Float constant for minus one.static final Float
Reusable Float constant for one.static final Float
Reusable Float constant for zero.static final Integer
Reusable Integer constant for minus one.static final Integer
Reusable Integer constant for one.static final Integer
Reusable Integer constant for twostatic final Integer
Reusable Integer constant for zero.static final Long
Reusable Long constant for minus one.static final Long
Reusable Long constant for one.static final Long
Reusable Long constant for zero.static final Short
Reusable Short constant for minus one.static final Short
Reusable Short constant for one.static final Short
Reusable Short constant for zero. -
Method Summary
Modifier and TypeMethodDescriptionstatic double
acosh
(double a) Compute the inverse hyperbolic cosine of a number.static int
addExact
(int a, int b) Returns the sum ofa
andb
, provided it does not overflow.static long
addExact
(long a, long b) Returns the sum ofa
andb
, 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 int
binomial
(int n, int k) Returnsn
choosek
, also known as the binomial coefficient ofn
andk
, orInteger.MAX_VALUE
if the result does not fit in anint
.static BigInteger
binomialToBigInteger
(int n, int k) Returnsn
choosek
, also known as the binomial coefficient ofn
andk
, that is,n! / (k! (n - k)!)
.static long
binomialToLong
(int n, int k) Returnsn
choosek
, also known as the binomial coefficient ofn
andk
, orLong.MAX_VALUE
if the result does not fit in along
.static int
castExact
(long value) Returns theint
value that is equal tovalue
, if possible.static long
ceilingPowerOfTwo
(long x) Returns the smallest power of two greater than or equal tox
.static BigInteger
Ceiling power of two.static <T extends Number>
Tstatic <T extends Number>
Tstatic BigDecimal
createBigDecimal
(String str) Convert aString
to aBigDecimal
.static BigInteger
createBigInteger
(String str) Convert aString
to aBigInteger
; since 3.2 it handles hex (0x or #) and octal (0) notations.static Double
createDouble
(String str) Convert aString
to aDouble
.static Float
createFloat
(String str) Convert aString
to aFloat
.static Integer
createInteger
(String str) Convert aString
to aInteger
, handling hex (0xhhhh) and octal (0dddd) notations.static Long
createLong
(String str) Convert aString
to aLong
; since 3.1 it handles hex (0Xhhhh) and octal (0ddd) notations.static Number
createNumber
(String str) Turns a string value into a java.lang.Number.static int
divide
(int p, int q, RoundingMode mode) Returns the result of dividingp
byq
, rounding using the specifiedRoundingMode
.static long
divide
(long p, long q, RoundingMode mode) Returns the result of dividingp
byq
, rounding using the specifiedRoundingMode
.static BigInteger
divide
(BigInteger p, BigInteger q, RoundingMode mode) static int
factorial
(int n) Returnsn!
, that is, the product of the firstn
positive integers,1
ifn == 0
, orInteger.MAX_VALUE
if the result does not fit in aint
.static BigInteger
factorialToBigInteger
(int n) Returnsn!
, that is, the product of the firstn
positive integers, or1
ifn == 0
.static double
factorialToDouble
(int n) Returnsn!
, that is, the product of the firstn
positive integers,1
ifn == 0
, orn!
, orDouble.POSITIVE_INFINITY
ifn! > Double.MAX_VALUE
.static long
factorialToLong
(int n) Returnsn!
, that is, the product of the firstn
positive integers,1
ifn == 0
, orLong.MAX_VALUE
if the result does not fit in along
.static long
floorPowerOfTwo
(long x) Returns the largest power of two less than or equal tox
.static BigInteger
Floor power of two.static String
Numbers.format(val, "0.0"); --> 12.1 Numbers.format(val, "#.#"); --> 12.1 Numbers.format(val, "0.00"); --> 12.10 Numbers.format(val, "#.##"); --> 12.1 Numbers.format(val, "0.000"); --> 12.105 Numbers.format(val, "#.###"); --> 12.105 Numbers.format(val, "0.0000"); --> 12.1050 Numbers.format(val, "#.####"); --> 12.105 Numbers.format(val, "0.00000"); --> 12.10500 Numbers.format(val, "#.#####"); --> 12.105
static String
Numbers.format(val, "0.0"); --> 12.1 Numbers.format(val, "#.#"); --> 12.1 Numbers.format(val, "0.00"); --> 12.10 Numbers.format(val, "#.##"); --> 12.1 Numbers.format(val, "0.000"); --> 12.105 Numbers.format(val, "#.###"); --> 12.105 Numbers.format(val, "0.0000"); --> 12.1050 Numbers.format(val, "#.####"); --> 12.105 Numbers.format(val, "0.00000"); --> 12.10500 Numbers.format(val, "#.#####"); --> 12.105
static String
static String
static int
fuzzyCompare
(double a, double b, double tolerance) Comparesa
andb
"fuzzily," with a tolerance for nearly-equal values.static boolean
fuzzyEquals
(double a, double b, double tolerance) Returnstrue
ifa
andb
are withintolerance
of each other.static int
gcd
(int a, int b) Returns the greatest common divisor ofa, b
.static long
gcd
(long a, long b) Returns the greatest common divisor ofa, b
.static boolean
isCreatable
(String str) Checks whether the String a valid Java number.static boolean
Checks whether theString
contains only digit characters.static boolean
isMathematicalInteger
(double x) Returnstrue
ifx
represents a mathematical integer.static boolean
Note: It's copied from NumberUtils in Apache Commons Lang under Apache License 2.0
It's same asisCreatable(String)
.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
(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
Returnstrue
ifx
represents a power of two.static boolean
isPrime
(long n) Returnstrue
ifn
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 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 double
log
(double a) static double
log10
(double x) static int
log10
(int x, RoundingMode mode) static int
log10
(long x, RoundingMode mode) Returns the base-10 logarithm ofx
, rounded according to the specified rounding mode.static int
log10
(BigInteger x, RoundingMode mode) Returns the base-10 logarithm ofx
, 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 anint
.static int
log2
(int x, RoundingMode mode) static int
log2
(long x, RoundingMode mode) Returns the base-2 logarithm ofx
, rounded according to the specified rounding mode.static int
log2
(BigInteger x, RoundingMode mode) Returns the base-2 logarithm ofx
, 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 ofvalues
.static int
mean
(int x, int y) Returns the arithmetic mean ofx
andy
, rounded towards negative infinity.static double
mean
(long... values) Returns the arithmetic mean ofvalues
.static long
mean
(long x, long y) Returns the arithmetic mean ofx
andy
, rounded toward negative infinity.static int
mod
(int x, int m) Returnsx mod m
, a non-negative value less thanm
.static int
mod
(long x, int m) Returnsx mod m
, a non-negative value less thanm
.static long
mod
(long x, long m) Returnsx mod m
, a non-negative value less thanm
.static int
multiplyExact
(int a, int b) Returns the product ofa
andb
, provided it does not overflow.static long
multiplyExact
(long a, long b) Returns the product ofa
andb
, provided it does not overflow.static int
pow
(int b, int k) static long
pow
(long b, int k) Returnsb
to thek
th power.static int
powExact
(int b, int k) Returns theb
to thek
th power, provided it does not overflow.static long
powExact
(long b, int k) Returns theb
to thek
th power, provided it does not overflow.static double
round
(double x, int scale) static double
round
(double x, int scale, RoundingMode roundingMode) static double
static double
round
(double x, DecimalFormat decimalFormat) static float
round
(float x, int scale) static float
round
(float x, int scale, RoundingMode roundingMode) static float
static float
round
(float x, DecimalFormat decimalFormat) static BigInteger
roundToBigInteger
(double x, RoundingMode mode) Returns theBigInteger
value that is equal tox
rounded with the specified rounding mode, if possible.static int
roundToInt
(double x, RoundingMode mode) Returns theint
value that is equal tox
rounded with the specified rounding mode, if possible.static long
roundToLong
(double x, RoundingMode mode) Returns thelong
value that is equal tox
rounded with the specified rounding mode, if possible.static int
saturatedAdd
(int a, int b) Returns the sum ofa
andb
unless it would overflow or underflow in which caseInteger.MAX_VALUE
orInteger.MIN_VALUE
is returned, respectively.static long
saturatedAdd
(long a, long b) Returns the sum ofa
andb
unless it would overflow or underflow in which caseLong.MAX_VALUE
orLong.MIN_VALUE
is returned, respectively.static int
saturatedCast
(long value) Returns theint
nearest in value tovalue
.static int
saturatedMultiply
(int a, int b) Returns the product ofa
andb
unless it would overflow or underflow in which caseInteger.MAX_VALUE
orInteger.MIN_VALUE
is returned, respectively.static long
saturatedMultiply
(long a, long b) Returns the product ofa
andb
unless it would overflow or underflow in which caseLong.MAX_VALUE
orLong.MIN_VALUE
is returned, respectively.static int
saturatedPow
(int b, int k) Returns theb
to thek
th power, unless it would overflow or underflow in which caseInteger.MAX_VALUE
orInteger.MIN_VALUE
is returned, respectively.static long
saturatedPow
(long b, int k) Returns theb
to thek
th power, unless it would overflow or underflow in which caseLong.MAX_VALUE
orLong.MIN_VALUE
is returned, respectively.static int
saturatedSubtract
(int a, int b) Returns the difference ofa
andb
unless it would overflow or underflow in which caseInteger.MAX_VALUE
orInteger.MIN_VALUE
is returned, respectively.static long
saturatedSubtract
(long a, long b) Returns the difference ofa
andb
unless it would overflow or underflow in which caseLong.MAX_VALUE
orLong.MIN_VALUE
is returned, respectively.static int
sqrt
(int x, RoundingMode mode) Returns the square root ofx
, rounded with the specified rounding mode.static long
sqrt
(long x, RoundingMode mode) Returns the square root ofx
, rounded with the specified rounding mode.static BigInteger
sqrt
(BigInteger x, RoundingMode mode) static int
subtractExact
(int a, int b) Returns the difference ofa
andb
, provided it does not overflow.static long
subtractExact
(long a, long b) Returns the difference ofa
andb
, provided it does not overflow.static byte
static byte
static byte
static byte
static double
static double
static double
static double
static double
toDouble
(BigDecimal value) Convert aBigDecimal
to adouble
.static double
toDouble
(BigDecimal value, double defaultValueForNull) Convert aBigDecimal
to adouble
.static float
static float
static float
static float
static int
static int
static int
static int
static int
toIntExact
(long value) Returns the value of thelong
argument; throwing an exception if the value overflows anint
.static long
static long
static long
static long
static BigDecimal
toScaledBigDecimal
(Double value) Convert aDouble
to aBigDecimal
with a scale of two that has been rounded usingRoundingMode.HALF_EVEN
.static BigDecimal
toScaledBigDecimal
(Double value, int scale, RoundingMode roundingMode) Convert aDouble
to aBigDecimal
whose scale is the specified value with aRoundingMode
applied.static BigDecimal
toScaledBigDecimal
(Float value) Convert aFloat
to aBigDecimal
with a scale of two that has been rounded usingRoundingMode.HALF_EVEN
.static BigDecimal
toScaledBigDecimal
(Float value, int scale, RoundingMode roundingMode) Convert aFloat
to aBigDecimal
whose scale is the specified value with aRoundingMode
applied.static BigDecimal
toScaledBigDecimal
(String value) Convert aString
to aBigDecimal
with a scale of two that has been rounded usingRoundingMode.HALF_EVEN
.static BigDecimal
toScaledBigDecimal
(String value, int scale, RoundingMode roundingMode) Convert aString
to aBigDecimal
whose scale is the specified value with aRoundingMode
applied.static BigDecimal
toScaledBigDecimal
(BigDecimal value) Convert aBigDecimal
to aBigDecimal
with a scale of two that has been rounded usingRoundingMode.HALF_EVEN
.static BigDecimal
toScaledBigDecimal
(BigDecimal value, int scale, RoundingMode roundingMode) Convert aBigDecimal
to aBigDecimal
whose scale is the specified value with aRoundingMode
applied.static short
static short
static short
static short
-
Field Details
-
BYTE_ZERO
Reusable Byte constant for zero. -
BYTE_ONE
Reusable Byte constant for one. -
BYTE_MINUS_ONE
Reusable Byte constant for minus one. -
SHORT_ZERO
Reusable Short constant for zero. -
SHORT_ONE
Reusable Short constant for one. -
SHORT_MINUS_ONE
Reusable Short constant for minus one. -
INTEGER_ZERO
Reusable Integer constant for zero. -
INTEGER_ONE
Reusable Integer constant for one. -
INTEGER_TWO
Reusable Integer constant for two -
INTEGER_MINUS_ONE
Reusable Integer constant for minus one. -
LONG_ZERO
Reusable Long constant for zero. -
LONG_ONE
Reusable Long constant for one. -
LONG_MINUS_ONE
Reusable Long constant for minus one. -
FLOAT_ZERO
Reusable Float constant for zero. -
FLOAT_ONE
Reusable Float constant for one. -
FLOAT_MINUS_ONE
Reusable Float constant for minus one. -
DOUBLE_ZERO
Reusable Double constant for zero. -
DOUBLE_ONE
Reusable Double constant for one. -
DOUBLE_MINUS_ONE
Reusable Double constant for minus one.
-
-
Method Details
-
convert
- Type Parameters:
T
-- Parameters:
value
-targetType
-- Returns:
-
convert
public static <T extends Number> T convert(Number value, com.landawn.abacus.type.Type<? extends T> targetType) - Type Parameters:
T
-- Parameters:
value
-targetType
-- Returns:
-
format
Numbers.format(val, "0.0"); --> 12.1 Numbers.format(val, "#.#"); --> 12.1 Numbers.format(val, "0.00"); --> 12.10 Numbers.format(val, "#.##"); --> 12.1 Numbers.format(val, "0.000"); --> 12.105 Numbers.format(val, "#.###"); --> 12.105 Numbers.format(val, "0.0000"); --> 12.1050 Numbers.format(val, "#.####"); --> 12.105 Numbers.format(val, "0.00000"); --> 12.10500 Numbers.format(val, "#.#####"); --> 12.105
- Parameters:
x
-decimalFormat
-- Returns:
- See Also:
-
format
- Parameters:
x
-decimalFormat
-- Returns:
- See Also:
-
format
Numbers.format(val, "0.0"); --> 12.1 Numbers.format(val, "#.#"); --> 12.1 Numbers.format(val, "0.00"); --> 12.10 Numbers.format(val, "#.##"); --> 12.1 Numbers.format(val, "0.000"); --> 12.105 Numbers.format(val, "#.###"); --> 12.105 Numbers.format(val, "0.0000"); --> 12.1050 Numbers.format(val, "#.####"); --> 12.105 Numbers.format(val, "0.00000"); --> 12.10500 Numbers.format(val, "#.#####"); --> 12.105
- Parameters:
x
-decimalFormat
-- Returns:
- See Also:
-
format
- Parameters:
x
-decimalFormat
-- Returns:
- See Also:
-
toByte
- Parameters:
str
-- Returns:
0
if the specifiedstr
is null or empty.- Throws:
NumberFormatException
- If the string is not a parsablebyte
.- See Also:
-
toByte
- Parameters:
obj
-- Returns:
0
if the specifiedobj
is null.- Throws:
NumberFormatException
- If the specifiedobj
is not aNumber
andobj.toString()
is not a parsablebyte
.- See Also:
-
toByte
- Parameters:
str
-defaultValueForNull
-- Returns:
defaultValue
if the specifiedstr
is null or empty.- Throws:
NumberFormatException
- If the string is not a parsablebyte
.- See Also:
-
toByte
- Parameters:
obj
-defaultValueForNull
-- Returns:
defaultValueForNull
if the specifiedobj
is null.- Throws:
NumberFormatException
- If the specifiedobj
is not aNumber
andobj.toString()
is not a parsablebyte
.- See Also:
-
toShort
- Parameters:
str
-- Returns:
0
if the specifiedstr
is null or empty.- Throws:
NumberFormatException
- If the string is not a parsableshort
.- See Also:
-
toShort
- Parameters:
obj
-- Returns:
0
if the specifiedobj
is null.- Throws:
NumberFormatException
- If the specifiedobj
is not aNumber
andobj.toString()
is not a parsableshort
.- See Also:
-
toShort
- Parameters:
str
-defaultValueForNull
-- Returns:
defaultValue
if the specifiedstr
is null or empty.- Throws:
NumberFormatException
- If the string is not a parsableshort
.- See Also:
-
toShort
- Parameters:
obj
-defaultValueForNull
-- Returns:
defaultValueForNull
if the specifiedobj
is null.- Throws:
NumberFormatException
- If the specifiedobj
is not aNumber
andobj.toString()
is not a parsableshort
.- See Also:
-
toInt
- Parameters:
str
-- Returns:
0
if the specifiedstr
is null or empty.- Throws:
NumberFormatException
- If the string is not a parsableint
.- See Also:
-
toInt
- Parameters:
obj
-- Returns:
0
if the specifiedobj
is null.- Throws:
NumberFormatException
- If the specifiedobj
is not aNumber
andobj.toString()
is not a parsableint
.- See Also:
-
toInt
- Parameters:
str
-defaultValueForNull
-- Returns:
defaultValue
if the specifiedstr
is null or empty.- Throws:
NumberFormatException
- If the string is not a parsableint
.- See Also:
-
toInt
- Parameters:
obj
-defaultValueForNull
-- Returns:
defaultValueForNull
if the specifiedobj
is null.- Throws:
NumberFormatException
- If the specifiedobj
is not aNumber
andobj.toString()
is not a parsableint
.- See Also:
-
#toInteger(String, int)
-
toLong
- Parameters:
str
-- Returns:
0
if the specifiedstr
is null or empty.- Throws:
NumberFormatException
- If the string is not a parsablelong
.- See Also:
-
toLong
- Parameters:
obj
-- Returns:
0
if the specifiedobj
is null.- Throws:
NumberFormatException
- If the specifiedobj
is not aNumber
andobj.toString()
is not a parsablelong
.- See Also:
-
toLong
- Parameters:
str
-defaultValueForNull
-- Returns:
defaultValue
if the specifiedstr
is null or empty.- Throws:
NumberFormatException
- If the string is not a parsablelong
.- See Also:
-
toLong
- Parameters:
obj
-defaultValueForNull
-- Returns:
defaultValueForNull
if the specifiedobj
is null.- Throws:
NumberFormatException
- If the specifiedobj
is not aNumber
andobj.toString()
is not a parsablelong
.- See Also:
-
toFloat
- Parameters:
str
-- Returns:
0
if the specifiedstr
is null or empty.- Throws:
NumberFormatException
- If the string is not a parsablefloat
.- See Also:
-
toFloat
- Parameters:
obj
-- Returns:
0
if the specifiedobj
is null.- Throws:
NumberFormatException
- If the specifiedobj
is not aNumber
andobj.toString()
is not a parsablefloat
.- See Also:
-
toFloat
- Parameters:
str
-defaultValueForNull
-- Returns:
defaultValue
if the specifiedstr
is null or empty.- Throws:
NumberFormatException
- If the string is not a parsablefloat
.- See Also:
-
toFloat
- Parameters:
obj
-defaultValueForNull
-- Returns:
defaultValueForNull
if the specifiedobj
is null.- Throws:
NumberFormatException
- If the specifiedobj
is not aNumber
andobj.toString()
is not a parsablefloat
.- See Also:
-
toDouble
- Parameters:
str
-- Returns:
0.0d
if the specifiedstr
is null or empty.- Throws:
NumberFormatException
- If the string is not a parsabledouble
.- See Also:
-
toDouble
- Parameters:
obj
-- Returns:
0
if the specifiedobj
is null.- Throws:
NumberFormatException
- If the specifiedobj
is not aNumber
andobj.toString()
is not a parsabledouble
.- See Also:
-
toDouble
- Parameters:
str
-defaultValueForNull
-- Returns:
defaultValue
if the specifiedstr
is null or empty.- Throws:
NumberFormatException
- If the string is not a parsabledouble
.- See Also:
-
toDouble
- Parameters:
obj
-defaultValueForNull
-- Returns:
defaultValueForNull
if the specifiedobj
is null.- Throws:
NumberFormatException
- If the specifiedobj
is not aNumber
andobj.toString()
is not a parsabledouble
.- See Also:
-
toDouble
Convert a
BigDecimal
to adouble
.If the
BigDecimal
value
isnull
, then the specified default value is returned.NumberUtils.toDouble(null) = 0.0d NumberUtils.toDouble(BigDecimal.valudOf(8.5d)) = 8.5d
- Parameters:
value
- theBigDecimal
to convert, may benull
.- Returns:
- the double represented by the
BigDecimal
or0.0d
if theBigDecimal
isnull
. - Since:
- 3.8
-
toDouble
Convert a
BigDecimal
to adouble
.If the
BigDecimal
value
isnull
, then the specified default value is returned.NumberUtils.toDouble(null, 1.1d) = 1.1d NumberUtils.toDouble(BigDecimal.valudOf(8.5d), 1.1d) = 8.5d
- Parameters:
value
- theBigDecimal
to convert, may benull
.defaultValueForNull
-- Returns:
- the double represented by the
BigDecimal
or the defaultValue if theBigDecimal
isnull
. - Since:
- 3.8
-
toScaledBigDecimal
Convert aBigDecimal
to aBigDecimal
with a scale of two that has been rounded usingRoundingMode.HALF_EVEN
. If the suppliedvalue
is null, thenBigDecimal.ZERO
is returned.Note, the scale of a
BigDecimal
is the number of digits to the right of the decimal point.- Parameters:
value
- theBigDecimal
to convert, may be null.- Returns:
- the scaled, with appropriate rounding,
BigDecimal
. - Since:
- 3.8
-
toScaledBigDecimal
Convert aBigDecimal
to aBigDecimal
whose scale is the specified value with aRoundingMode
applied. If the inputvalue
isnull
, we simply returnBigDecimal.ZERO
.- Parameters:
value
- theBigDecimal
to convert, may be null.scale
- the number of digits to the right of the decimal point.roundingMode
- a rounding behavior for numerical operations capable of discarding precision.- Returns:
- the scaled, with appropriate rounding,
BigDecimal
. - Since:
- 3.8
-
toScaledBigDecimal
Convert aFloat
to aBigDecimal
with a scale of two that has been rounded usingRoundingMode.HALF_EVEN
. If the suppliedvalue
is null, thenBigDecimal.ZERO
is returned.Note, the scale of a
BigDecimal
is the number of digits to the right of the decimal point.- Parameters:
value
- theFloat
to convert, may be null.- Returns:
- the scaled, with appropriate rounding,
BigDecimal
. - Since:
- 3.8
-
toScaledBigDecimal
Convert aFloat
to aBigDecimal
whose scale is the specified value with aRoundingMode
applied. If the inputvalue
isnull
, we simply returnBigDecimal.ZERO
.- Parameters:
value
- theFloat
to convert, may be null.scale
- the number of digits to the right of the decimal point.roundingMode
- a rounding behavior for numerical operations capable of discarding precision.- Returns:
- the scaled, with appropriate rounding,
BigDecimal
. - Since:
- 3.8
-
toScaledBigDecimal
Convert aDouble
to aBigDecimal
with a scale of two that has been rounded usingRoundingMode.HALF_EVEN
. If the suppliedvalue
is null, thenBigDecimal.ZERO
is returned.Note, the scale of a
BigDecimal
is the number of digits to the right of the decimal point.- Parameters:
value
- theDouble
to convert, may be null.- Returns:
- the scaled, with appropriate rounding,
BigDecimal
. - Since:
- 3.8
-
toScaledBigDecimal
Convert aDouble
to aBigDecimal
whose scale is the specified value with aRoundingMode
applied. If the inputvalue
isnull
, we simply returnBigDecimal.ZERO
.- Parameters:
value
- theDouble
to convert, may be null.scale
- the number of digits to the right of the decimal point.roundingMode
- a rounding behavior for numerical operations capable of discarding precision.- Returns:
- the scaled, with appropriate rounding,
BigDecimal
. - Since:
- 3.8
-
toScaledBigDecimal
Convert aString
to aBigDecimal
with a scale of two that has been rounded usingRoundingMode.HALF_EVEN
. If the suppliedvalue
is null, thenBigDecimal.ZERO
is returned.Note, the scale of a
BigDecimal
is the number of digits to the right of the decimal point.- Parameters:
value
- theString
to convert, may be null.- Returns:
- the scaled, with appropriate rounding,
BigDecimal
. - Since:
- 3.8
-
toScaledBigDecimal
Convert aString
to aBigDecimal
whose scale is the specified value with aRoundingMode
applied. If the inputvalue
isnull
, we simply returnBigDecimal.ZERO
.- Parameters:
value
- theString
to convert, may be null.scale
- the number of digits to the right of the decimal point.roundingMode
- a rounding behavior for numerical operations capable of discarding precision.- Returns:
- the scaled, with appropriate rounding,
BigDecimal
. - Since:
- 3.8
-
toIntExact
public static int toIntExact(long value) Returns the value of thelong
argument; throwing an exception if the value overflows anint
.- Parameters:
value
- the long value- Returns:
- Throws:
ArithmeticException
- if theargument
overflows an int- See Also:
-
createInteger
Convert a
String
to aInteger
, handling hex (0xhhhh) and octal (0dddd) notations. N.B. a leading zero means octal; spaces are not trimmed.null
is returned if the specifiedstr
isnull
.
- Parameters:
str
- aString
to convert, may be null- Returns:
null
if(str == null)
. (auto-generated java doc for return)- Throws:
NumberFormatException
- if the value cannot be converted- See Also:
-
createLong
Convert a
String
to aLong
; since 3.1 it handles hex (0Xhhhh) and octal (0ddd) notations. N.B. a leading zero means octal; spaces are not trimmed.null
is returned if the specifiedstr
isnull
.
- Parameters:
str
- aString
to convert, may be null- Returns:
null
if(str == null)
. (auto-generated java doc for return)- Throws:
NumberFormatException
- if the value cannot be converted- See Also:
-
createFloat
Convert a
String
to aFloat
.null
is returned if the specifiedstr
isnull
.
- Parameters:
str
- aString
to convert, may be null- Returns:
null
if(str == null)
. (auto-generated java doc for return)- Throws:
NumberFormatException
- if the value cannot be converted- See Also:
-
createDouble
Convert a
String
to aDouble
.null
is returned if the specifiedstr
isnull
.
- Parameters:
str
- aString
to convert, may be null- Returns:
null
if(str == null)
. (auto-generated java doc for return)- Throws:
NumberFormatException
- if the value cannot be converted- See Also:
-
createBigInteger
Convert a
String
to aBigInteger
; since 3.2 it handles hex (0x or #) and octal (0) notations.null
is returned if the specifiedstr
isnull
.
- Parameters:
str
- aString
to convert, may be null- Returns:
null
if(str == null)
. (auto-generated java doc for return)- Throws:
NumberFormatException
- if the value cannot be converted- See Also:
-
createBigDecimal
Convert a
String
to aBigDecimal
.null
is returned if the specifiedstr
isnull
.
- Parameters:
str
- aString
to convert, may be null- Returns:
null
if(str == null)
. (auto-generated java doc for return)- Throws:
NumberFormatException
- if the value cannot be converted- See Also:
-
createNumber
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
toBigInteger
and fromdouble
toBigDecimal
.Integral values with a leading
0
will be interpreted as octal; the returned number will be Integer, Long or BigDecimal as appropriate.null
is returned if the specifiedstr
isnull
.
- Parameters:
str
- a String containing a number, may be null- Returns:
null
if(str == null)
. (auto-generated java doc for return)- Throws:
NumberFormatException
- if the value cannot be converted- See Also:
-
isDigits
Checks whether theString
contains only digit characters.null
and empty String will returnfalse
.- Parameters:
str
- theString
to check- Returns:
true
if str contains only Unicode numeric- See Also:
-
isNumber
Note: It's copied from NumberUtils in Apache Commons Lang under Apache License 2.0
It's same asisCreatable(String)
.- See Also:
-
isCreatable
Checks whether the String a valid Java number.
Valid numbers include hexadecimal marked with the
0x
or0X
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 returnfalse
, since9
is not a valid octal value. However, numbers beginning with0.
are treated as decimal.null
and empty/blankString
will returnfalse
.Note,
createNumber(String)
should return a number for every input resulting intrue
.- Parameters:
str
- theString
to check- Returns:
true
if the string is a correctly formatted number- See Also:
-
isParsable
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)
orDouble.parseDouble(String)
. This method can be used instead of catchingNumberFormatException
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 returnfalse
.- Parameters:
str
- the String to check.- Returns:
true
if the string is a parsable number.- Since:
- 3.4
-
isPrime
public static boolean isPrime(long n) Returnstrue
ifn
is a prime number: an integer greater than one that cannot be factored into a product of smaller positive integers. Returnsfalse
ifn
is zero, one, or a composite number (one which can be factored into smaller positive integers).To test larger numbers, use
BigInteger.isProbablePrime(int)
.- Parameters:
n
-- Returns:
- true, if is prime
- Throws:
IllegalArgumentException
- ifn
is negative- Since:
- 20.0
-
isPerfectSquare
public static boolean isPerfectSquare(int n) Checks if is perfect square.- Parameters:
n
-- Returns:
- true, if is perfect square
-
isPerfectSquare
public static boolean isPerfectSquare(long n) Checks if is perfect square.- Parameters:
n
-- Returns:
- true, if is perfect square
-
isPowerOfTwo
public static boolean isPowerOfTwo(int x) Checks if is power of two.- Parameters:
x
-- Returns:
- true, if is power of two
-
isPowerOfTwo
public static boolean isPowerOfTwo(long x) Checks if is power of two.- Parameters:
x
-- Returns:
- true, if is power of two
-
isPowerOfTwo
public static boolean isPowerOfTwo(double x) Checks if is power of two.- Parameters:
x
-- Returns:
- true, if is power of two
-
isPowerOfTwo
Returnstrue
ifx
represents a power of two.- Parameters:
x
-- Returns:
- true, if is power of two
-
log
public static double log(double a) - Parameters:
a
-- Returns:
-
log2
- Parameters:
x
-mode
-- Returns:
-
log2
Returns the base-2 logarithm ofx
, rounded according to the specified rounding mode.- Parameters:
x
-mode
-- Returns:
- Throws:
IllegalArgumentException
- ifx <= 0
ArithmeticException
- ifmode
isRoundingMode.UNNECESSARY
andx
is not a power of two
-
log2
public static double log2(double x) Returns the base 2 logarithm of a double value.Special cases:
- If
x
is NaN or less than zero, the result is NaN. - If
x
is positive infinity, the result is positive infinity. - If
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.- Parameters:
x
-- Returns:
- If
-
log2
Returns the base 2 logarithm of a double value, rounded with the specified rounding mode to anint
.Regardless of the rounding mode, this is faster than
(int) log2(x)
.- Parameters:
x
-mode
-- Returns:
- Throws:
IllegalArgumentException
- ifx <= 0.0
,x
is NaN, orx
is infinite
-
log2
Returns the base-2 logarithm ofx
, rounded according to the specified rounding mode.- Parameters:
x
-mode
-- Returns:
- Throws:
IllegalArgumentException
- ifx <= 0
ArithmeticException
- ifmode
isRoundingMode.UNNECESSARY
andx
is not a power of two
-
log10
- Parameters:
x
-mode
-- Returns:
-
log10
Returns the base-10 logarithm ofx
, rounded according to the specified rounding mode.- Parameters:
x
-mode
-- Returns:
- Throws:
IllegalArgumentException
- ifx <= 0
ArithmeticException
- ifmode
isRoundingMode.UNNECESSARY
andx
is not a power of ten
-
log10
public static double log10(double x) - Parameters:
x
-- Returns:
-
log10
Returns the base-10 logarithm ofx
, rounded according to the specified rounding mode.- Parameters:
x
-mode
-- Returns:
- Throws:
IllegalArgumentException
- ifx <= 0
ArithmeticException
- ifmode
isRoundingMode.UNNECESSARY
andx
is not a power of ten
-
pow
public static int pow(int b, int k) - Parameters:
b
-k
-- Returns:
-
pow
public static long pow(long b, int k) Returnsb
to thek
th power. Even if the result overflows, it will be equal toBigInteger.valueOf(b).pow(k).longValue()
. This implementation runs inO(log k)
time.- Parameters:
b
-k
-- Returns:
- Throws:
IllegalArgumentException
- ifk < 0
-
ceilingPowerOfTwo
public static long ceilingPowerOfTwo(long x) Returns the smallest power of two greater than or equal tox
. This is equivalent tocheckedPow(2, log2(x, CEILING))
.- Parameters:
x
-- Returns:
- Throws:
IllegalArgumentException
- ifx <= 0
ArithmeticException
- of the next-higher power of two is not representable as along
, i.e. whenx > 2^62
- Since:
- 20.0
-
ceilingPowerOfTwo
Ceiling power of two.- Parameters:
x
-- Returns:
-
floorPowerOfTwo
public static long floorPowerOfTwo(long x) Returns the largest power of two less than or equal tox
. This is equivalent tocheckedPow(2, log2(x, FLOOR))
.- Parameters:
x
-- Returns:
- Throws:
IllegalArgumentException
- ifx <= 0
- Since:
- 20.0
-
floorPowerOfTwo
Floor power of two.- Parameters:
x
-- Returns:
-
sqrt
Returns the square root ofx
, rounded with the specified rounding mode.- Parameters:
x
-mode
-- Returns:
- Throws:
IllegalArgumentException
- ifx < 0
ArithmeticException
- ifmode
isRoundingMode.UNNECESSARY
andsqrt(x)
is not an integer
-
sqrt
Returns the square root ofx
, rounded with the specified rounding mode.- Parameters:
x
-mode
-- Returns:
- Throws:
IllegalArgumentException
- ifx < 0
ArithmeticException
- ifmode
isRoundingMode.UNNECESSARY
andsqrt(x)
is not an integer
-
sqrt
- Parameters:
x
-mode
-- Returns:
-
divide
Returns the result of dividingp
byq
, rounding using the specifiedRoundingMode
.- Parameters:
p
-q
-mode
-- Returns:
- Throws:
ArithmeticException
- ifq == 0
, or ifmode == UNNECESSARY
anda
is not an integer multiple ofb
-
divide
Returns the result of dividingp
byq
, rounding using the specifiedRoundingMode
.- Parameters:
p
-q
-mode
-- Returns:
- Throws:
ArithmeticException
- ifq == 0
, or ifmode == UNNECESSARY
anda
is not an integer multiple ofb
-
divide
- Parameters:
p
-q
-mode
-- Returns:
-
mod
public static int mod(int x, int m) Returnsx mod m
, a non-negative value less thanm
. This differs fromx % 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) == 0
- Parameters:
x
-m
-- Returns:
- Throws:
ArithmeticException
- ifm <= 0
- See Also:
-
mod
public static int mod(long x, int m) Returnsx mod m
, a non-negative value less thanm
. This differs fromx % 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) == 0
- Parameters:
x
-m
-- Returns:
- Throws:
ArithmeticException
- ifm <= 0
- See Also:
-
mod
public static long mod(long x, long m) Returnsx mod m
, a non-negative value less thanm
. This differs fromx % 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) == 0
- Parameters:
x
-m
-- Returns:
- Throws:
ArithmeticException
- ifm <= 0
- See Also:
-
gcd
public static int gcd(int a, int b) Returns the greatest common divisor ofa, b
. Returns0
ifa == 0 && b == 0
.- Parameters:
a
-b
-- Returns:
- Throws:
IllegalArgumentException
- ifa < 0
orb < 0
-
gcd
public static long gcd(long a, long b) Returns the greatest common divisor ofa, b
. Returns0
ifa == 0 && b == 0
.- Parameters:
a
-b
-- Returns:
- Throws:
IllegalArgumentException
- ifa < 0
orb < 0
-
lcm
Returns the least common multiple of the absolute value of two numbers, using the formula
Special cases:lcm(a,b) = (a / gcd(a,b)) * b
.- 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:
- Throws:
ArithmeticException
- if the result cannot be represented as a non-negativeint
value.- Since:
- 1.1
- The invocations
-
lcm
Returns the least common multiple of the absolute value of two numbers, using the formula
Special cases:lcm(a,b) = (a / gcd(a,b)) * b
.- 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:
- Throws:
ArithmeticException
- if the result cannot be represented as a non-negativelong
value.- Since:
- 2.1
- The invocations
-
addExact
public static int addExact(int a, int b) Returns the sum ofa
andb
, provided it does not overflow.- Parameters:
a
-b
-- Returns:
- Throws:
ArithmeticException
- ifa + b
overflows in signedint
arithmetic
-
addExact
public static long addExact(long a, long b) Returns the sum ofa
andb
, provided it does not overflow.- Parameters:
a
-b
-- Returns:
- Throws:
ArithmeticException
- ifa + b
overflows in signedlong
arithmetic
-
subtractExact
public static int subtractExact(int a, int b) Returns the difference ofa
andb
, provided it does not overflow.- Parameters:
a
-b
-- Returns:
- Throws:
ArithmeticException
- ifa - b
overflows in signedint
arithmetic
-
subtractExact
public static long subtractExact(long a, long b) Returns the difference ofa
andb
, provided it does not overflow.- Parameters:
a
-b
-- Returns:
- Throws:
ArithmeticException
- ifa - b
overflows in signedlong
arithmetic
-
multiplyExact
public static int multiplyExact(int a, int b) Returns the product ofa
andb
, provided it does not overflow.- Parameters:
a
-b
-- Returns:
- Throws:
ArithmeticException
- ifa * b
overflows in signedint
arithmetic
-
multiplyExact
public static long multiplyExact(long a, long b) Returns the product ofa
andb
, provided it does not overflow.- Parameters:
a
-b
-- Returns:
- Throws:
ArithmeticException
- ifa * b
overflows in signedlong
arithmetic
-
powExact
public static int powExact(int b, int k) Returns theb
to thek
th power, provided it does not overflow.pow(int, int)
may be faster, but does not check for overflow.- Parameters:
b
-k
-- Returns:
- Throws:
ArithmeticException
- ifb
to thek
th power overflows in signedint
arithmetic
-
powExact
public static long powExact(long b, int k) Returns theb
to thek
th power, provided it does not overflow.- Parameters:
b
-k
-- Returns:
- Throws:
ArithmeticException
- ifb
to thek
th power overflows in signedlong
arithmetic
-
castExact
public static int castExact(long value) Returns theint
value that is equal tovalue
, if possible.- Parameters:
value
- any value in the range of theint
type- Returns:
- Throws:
IllegalArgumentException
- ifvalue
is greater thanInteger.MAX_VALUE
or less thanInteger.MIN_VALUE
-
saturatedAdd
public static int saturatedAdd(int a, int b) Returns the sum ofa
andb
unless it would overflow or underflow in which caseInteger.MAX_VALUE
orInteger.MIN_VALUE
is returned, respectively.- Parameters:
a
-b
-- Returns:
- Since:
- 20.0
-
saturatedAdd
public static long saturatedAdd(long a, long b) Returns the sum ofa
andb
unless it would overflow or underflow in which caseLong.MAX_VALUE
orLong.MIN_VALUE
is returned, respectively.- Parameters:
a
-b
-- Returns:
- Since:
- 20.0
-
saturatedSubtract
public static int saturatedSubtract(int a, int b) Returns the difference ofa
andb
unless it would overflow or underflow in which caseInteger.MAX_VALUE
orInteger.MIN_VALUE
is returned, respectively.- Parameters:
a
-b
-- Returns:
- Since:
- 20.0
-
saturatedSubtract
public static long saturatedSubtract(long a, long b) Returns the difference ofa
andb
unless it would overflow or underflow in which caseLong.MAX_VALUE
orLong.MIN_VALUE
is returned, respectively.- Parameters:
a
-b
-- Returns:
- Since:
- 20.0
-
saturatedMultiply
public static int saturatedMultiply(int a, int b) Returns the product ofa
andb
unless it would overflow or underflow in which caseInteger.MAX_VALUE
orInteger.MIN_VALUE
is returned, respectively.- Parameters:
a
-b
-- Returns:
- Since:
- 20.0
-
saturatedMultiply
public static long saturatedMultiply(long a, long b) Returns the product ofa
andb
unless it would overflow or underflow in which caseLong.MAX_VALUE
orLong.MIN_VALUE
is returned, respectively.- Parameters:
a
-b
-- Returns:
- Since:
- 20.0
-
saturatedPow
public static int saturatedPow(int b, int k) Returns theb
to thek
th power, unless it would overflow or underflow in which caseInteger.MAX_VALUE
orInteger.MIN_VALUE
is returned, respectively.- Parameters:
b
-k
-- Returns:
- Since:
- 20.0
-
saturatedPow
public static long saturatedPow(long b, int k) Returns theb
to thek
th power, unless it would overflow or underflow in which caseLong.MAX_VALUE
orLong.MIN_VALUE
is returned, respectively.- Parameters:
b
-k
-- Returns:
- Since:
- 20.0
-
saturatedCast
public static int saturatedCast(long value) Returns theint
nearest in value tovalue
.- Parameters:
value
- anylong
value- Returns:
Integer.MAX_VALUE
if it is too large, orInteger.MIN_VALUE
if it is too small
-
factorial
public static int factorial(int n) Returnsn!
, that is, the product of the firstn
positive integers,1
ifn == 0
, orInteger.MAX_VALUE
if the result does not fit in aint
.- Parameters:
n
-- Returns:
- Throws:
IllegalArgumentException
- ifn < 0
-
factorialToLong
public static long factorialToLong(int n) Returnsn!
, that is, the product of the firstn
positive integers,1
ifn == 0
, orLong.MAX_VALUE
if the result does not fit in along
.- Parameters:
n
-- Returns:
- Throws:
IllegalArgumentException
- ifn < 0
-
factorialToDouble
public static double factorialToDouble(int n) Returnsn!
, that is, the product of the firstn
positive integers,1
ifn == 0
, orn!
, orDouble.POSITIVE_INFINITY
ifn! > Double.MAX_VALUE
.The result is within 1 ulp of the true value.
- Parameters:
n
-- Returns:
- Throws:
IllegalArgumentException
- ifn < 0
-
factorialToBigInteger
Returnsn!
, that is, the product of the firstn
positive integers, or1
ifn == 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).
- Parameters:
n
-- Returns:
- Throws:
IllegalArgumentException
- ifn < 0
-
binomial
public static int binomial(int n, int k) Returnsn
choosek
, also known as the binomial coefficient ofn
andk
, orInteger.MAX_VALUE
if the result does not fit in anint
.- Parameters:
n
-k
-- Returns:
- Throws:
IllegalArgumentException
- ifn < 0
,k < 0
ork > n
-
binomialToLong
public static long binomialToLong(int n, int k) Returnsn
choosek
, also known as the binomial coefficient ofn
andk
, orLong.MAX_VALUE
if the result does not fit in along
.- Parameters:
n
-k
-- Returns:
- Throws:
IllegalArgumentException
- ifn < 0
,k < 0
, ork > n
-
binomialToBigInteger
Returnsn
choosek
, also known as the binomial coefficient ofn
andk
, that is,n! / (k! (n - k)!)
.Warning: the result can take as much as O(k log n) space.
- Parameters:
n
-k
-- Returns:
- Throws:
IllegalArgumentException
- ifn < 0
,k < 0
, ork > n
-
mean
public static int mean(int x, int y) Returns the arithmetic mean ofx
andy
, rounded towards negative infinity. This method is overflow resilient.- Parameters:
x
-y
-- Returns:
- Since:
- 14.0
-
mean
public static long mean(long x, long y) Returns the arithmetic mean ofx
andy
, rounded toward negative infinity. This method is resilient to overflow.- Parameters:
x
-y
-- Returns:
- Since:
- 14.0
-
mean
public static double mean(double x, double y) - Parameters:
x
-y
-- Returns:
-
mean
Returns the arithmetic mean ofvalues
.If these values are a sample drawn from a population, this is also an unbiased estimator of the arithmetic mean of the population.
- Parameters:
values
- a nonempty series of values- Returns:
- Throws:
IllegalArgumentException
- ifvalues
is empty
-
mean
Returns the arithmetic mean ofvalues
.If these values are a sample drawn from a population, this is also an unbiased estimator of the arithmetic mean of the population.
- Parameters:
values
- a nonempty series of values, which will be converted todouble
values (this may cause loss of precision for longs of magnitude over 2^53 (slightly over 9e15))- Returns:
- Throws:
IllegalArgumentException
- ifvalues
is empty
-
mean
- Parameters:
values
-- Returns:
-
round
public static float round(float x, int scale) - Parameters:
x
-scale
-- Returns:
- See Also:
-
round
public static double round(double x, int scale) - Parameters:
x
-scale
-- Returns:
- See Also:
-
round
- Parameters:
x
-scale
-roundingMode
-- Returns:
- See Also:
-
round
- Parameters:
x
-scale
-roundingMode
-- Returns:
- See Also:
-
round
- Parameters:
x
-decimalFormat
-- Returns:
- See Also:
-
round
- Parameters:
x
-decimalFormat
-- Returns:
- See Also:
-
round
- Parameters:
x
-decimalFormat
-- Returns:
- See Also:
-
round
- Parameters:
x
-decimalFormat
-- Returns:
- See Also:
-
roundToInt
Returns theint
value that is equal tox
rounded with the specified rounding mode, if possible.- Parameters:
x
-mode
-- Returns:
- Throws:
ArithmeticException
- ifx
is infinite or NaNx
, after being rounded to a mathematical integer using the specified rounding mode, is either less thanInteger.MIN_VALUE
or greater thanInteger.MAX_VALUE
x
is not a mathematical integer andmode
isRoundingMode.UNNECESSARY
-
roundToLong
Returns thelong
value that is equal tox
rounded with the specified rounding mode, if possible.- Parameters:
x
-mode
-- Returns:
- Throws:
ArithmeticException
- ifx
is infinite or NaNx
, after being rounded to a mathematical integer using the specified rounding mode, is either less thanLong.MIN_VALUE
or greater thanLong.MAX_VALUE
x
is not a mathematical integer andmode
isRoundingMode.UNNECESSARY
-
roundToBigInteger
Returns theBigInteger
value that is equal tox
rounded with the specified rounding mode, if possible.- Parameters:
x
-mode
-- Returns:
- Throws:
ArithmeticException
- ifx
is infinite or NaNx
is not a mathematical integer andmode
isRoundingMode.UNNECESSARY
-
fuzzyEquals
public static boolean fuzzyEquals(double a, double b, double tolerance) Returnstrue
ifa
andb
are withintolerance
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:
- All NaNs are fuzzily equal.
- If
a == b
, thena
andb
are always fuzzily equal. - Positive and negative zero are always fuzzily equal.
- If
tolerance
is zero, and neithera
norb
is NaN, thena
andb
are fuzzily equal if and only ifa == b
. - With
Double.POSITIVE_INFINITY
tolerance, all non-NaN values are fuzzily equal. - With finite tolerance,
Double.POSITIVE_INFINITY
andDouble.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.
- Parameters:
a
-b
-tolerance
-- Returns:
- Throws:
IllegalArgumentException
- iftolerance
is< 0
or NaN- Since:
- 13.0
-
fuzzyCompare
public static int fuzzyCompare(double a, double b, double tolerance) Comparesa
andb
"fuzzily," with a tolerance for nearly-equal values.This method is equivalent to
fuzzyEquals(a, b, tolerance) ? 0 : Double.compare(a, b)
. In particular, likeDouble.compare(double, double)
, it treats all NaN values as equal and greater than all other values (includingDouble.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.- Parameters:
a
-b
-tolerance
-- Returns:
- Throws:
IllegalArgumentException
- iftolerance
is< 0
or NaN- Since:
- 13.0
-
isMathematicalInteger
public static boolean isMathematicalInteger(double x) Returnstrue
ifx
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)
.- Parameters:
x
-- Returns:
- true, if is mathematical integer
-
asinh
public static double asinh(double a) Compute the inverse hyperbolic sine of a number.- Parameters:
a
- number on which evaluation is done- Returns:
- inverse hyperbolic sine of a
-
acosh
public static double acosh(double a) Compute the inverse hyperbolic cosine of a number.- Parameters:
a
- number on which evaluation is done- Returns:
- inverse hyperbolic cosine of a
-
atanh
public static double atanh(double a) Compute the inverse hyperbolic tangent of a number.- Parameters:
a
- number on which evaluation is done- Returns:
- inverse hyperbolic tangent of a
-