Class DoubleMath
- java.lang.Object
-
- com.google.common.math.DoubleMath
-
public final class DoubleMath extends java.lang.Object
A class for arithmetic on doubles that is not covered byMath
.- Since:
- 11.0
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static double
factorial(int n)
Returnsn!
, that is, the product of the firstn
positive integers,1
ifn == 0
, or e n!}, 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, java.math.RoundingMode mode)
Returns the base 2 logarithm of a double value, rounded with the specified rounding mode to anint
.static double
mean(double... values)
Returns the arithmetic mean of the values.static double
mean(int... values)
Returns the arithmetic mean of the values.static double
mean(long... values)
Returns the arithmetic mean of the values.static double
mean(java.lang.Iterable<? extends java.lang.Number> values)
Returns the arithmetic mean of the values.static double
mean(java.util.Iterator<? extends java.lang.Number> values)
Returns the arithmetic mean of the values.static java.math.BigInteger
roundToBigInteger(double x, java.math.RoundingMode mode)
Returns theBigInteger
value that is equal tox
rounded with the specified rounding mode, if possible.static int
roundToInt(double x, java.math.RoundingMode mode)
Returns theint
value that is equal tox
rounded with the specified rounding mode, if possible.static long
roundToLong(double x, java.math.RoundingMode mode)
Returns thelong
value that is equal tox
rounded with the specified rounding mode, if possible.
-
-
-
Method Detail
-
roundToInt
public static int roundToInt(double x, java.math.RoundingMode mode)
Returns theint
value that is equal tox
rounded with the specified rounding mode, if possible.- Throws:
java.lang.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
public static long roundToLong(double x, java.math.RoundingMode mode)
Returns thelong
value that is equal tox
rounded with the specified rounding mode, if possible.- Throws:
java.lang.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
public static java.math.BigInteger roundToBigInteger(double x, java.math.RoundingMode mode)
Returns theBigInteger
value that is equal tox
rounded with the specified rounding mode, if possible.- Throws:
java.lang.ArithmeticException
- ifx
is infinite or NaNx
is not a mathematical integer andmode
isRoundingMode.UNNECESSARY
-
isPowerOfTwo
public static boolean isPowerOfTwo(double x)
Returnstrue
ifx
is exactly equal to2^k
for some finite integerk
.
-
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. - If
-
log2
public static int log2(double x, java.math.RoundingMode mode)
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:
java.lang.IllegalArgumentException
- ifx <= 0.0
,x
is NaN, orx
is infinite
-
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)
.
-
factorial
public static double factorial(int n)
Returnsn!
, that is, the product of the firstn
positive integers,1
ifn == 0
, or e n!}, orDouble.POSITIVE_INFINITY
ifn! > Double.MAX_VALUE
.The result is within 1 ulp of the true value.
- Throws:
java.lang.IllegalArgumentException
- ifn < 0
-
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.- Throws:
java.lang.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.- Throws:
java.lang.IllegalArgumentException
- iftolerance
is< 0
or NaN- Since:
- 13.0
-
mean
public static double mean(double... values)
Returns the arithmetic mean of the values. There must be at least one value, and they must all be finite.
-
mean
public static double mean(int... values)
Returns the arithmetic mean of the values. There must be at least one value. The values will be converted to doubles, which does not cause any loss of precision for ints.
-
mean
public static double mean(long... values)
Returns the arithmetic mean of the values. There must be at least one value. The values will be converted to doubles, which causes loss of precision for longs of magnitude over 2^53 (slightly over 9e15).
-
mean
public static double mean(java.lang.Iterable<? extends java.lang.Number> values)
Returns the arithmetic mean of the values. There must be at least one value, and they must all be finite. The values will be converted to doubles, which may cause loss of precision for some numeric types.
-
mean
public static double mean(java.util.Iterator<? extends java.lang.Number> values)
Returns the arithmetic mean of the values. There must be at least one value, and they must all be finite. The values will be converted to doubles, which may cause loss of precision for some numeric types.
-
-