Class DoubleMath
-
Method Summary
Modifier and TypeMethodDescriptionstatic double
factorial
(int n) Returnsn!
, that is, the product of the firstn
positive integers,1
ifn == 0
, orn!
, orDouble.POSITIVE_INFINITY
ifn! > Double.MAX_VALUE
.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 boolean
isMathematicalInteger
(double x) Returnstrue
ifx
represents a mathematical integer.static boolean
isPowerOfTwo
(double x) Returnstrue
ifx
is exactly equal to2^k
for some finite integerk
.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 double
mean
(double... values) Deprecated.static double
mean
(int... values) Deprecated.UseStats.meanOf(java.lang.Iterable<? extends java.lang.Number>)
instead, noting the less strict handling of non-finite values.static double
mean
(long... values) Deprecated.UseStats.meanOf(java.lang.Iterable<? extends java.lang.Number>)
instead, noting the less strict handling of non-finite values.static double
Deprecated.UseStats.meanOf(java.lang.Iterable<? extends java.lang.Number>)
instead, noting the less strict handling of non-finite values.static double
Deprecated.UseStats.meanOf(java.lang.Iterable<? extends java.lang.Number>)
instead, noting the less strict handling of non-finite values.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.
-
Method Details
-
roundToInt
Returns theint
value that is equal tox
rounded with the specified rounding mode, if possible.- 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.- 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.- Throws:
ArithmeticException
- ifx
is infinite or NaNx
is not a mathematical integer andmode
isRoundingMode.UNNECESSARY
-
isPowerOfTwo
Returnstrue
ifx
is exactly equal to2^k
for some finite integerk
. -
log2
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. - 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)
.- Throws:
IllegalArgumentException
- ifx <= 0.0
,x
is NaN, orx
is infinite
-
isMathematicalInteger
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)
. -
factorial
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.
- Throws:
IllegalArgumentException
- ifn < 0
-
fuzzyEquals
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.- Throws:
IllegalArgumentException
- iftolerance
is< 0
or NaN- Since:
- 13.0
-
fuzzyCompare
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.- Throws:
IllegalArgumentException
- iftolerance
is< 0
or NaN- Since:
- 13.0
-
mean
Deprecated.UseStats.meanOf(java.lang.Iterable<? extends java.lang.Number>)
instead, noting the less strict handling of non-finite values.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- Throws:
IllegalArgumentException
- ifvalues
is empty or contains any non-finite value
-
mean
Deprecated.UseStats.meanOf(java.lang.Iterable<? extends java.lang.Number>)
instead, noting the less strict handling of non-finite values.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- Throws:
IllegalArgumentException
- ifvalues
is empty
-
mean
Deprecated.UseStats.meanOf(java.lang.Iterable<? extends java.lang.Number>)
instead, noting the less strict handling of non-finite values.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))- Throws:
IllegalArgumentException
- ifvalues
is empty
-
mean
Deprecated.UseStats.meanOf(java.lang.Iterable<? extends java.lang.Number>)
instead, noting the less strict handling of non-finite values.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)- Throws:
IllegalArgumentException
- ifvalues
is empty or contains any non-finite value
-
mean
Deprecated.UseStats.meanOf(java.lang.Iterable<? extends java.lang.Number>)
instead, noting the less strict handling of non-finite values.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)- Throws:
IllegalArgumentException
- ifvalues
is empty or contains any non-finite value
-
Stats.meanOf(java.lang.Iterable<? extends java.lang.Number>)
instead, noting the less strict handling of non-finite values.