@GwtCompatible(emulated=true) public final class DoubleMath extends Object
Math.
| Modifier and Type | Method and Description |
|---|---|
static double |
factorial(int n)
Returns
n!, that is, the product of the first
n positive integers,
1 if
n == 0, or
n!, or
Double if
n! > Double.MAX_VALUE.
|
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 boolean |
isMathematicalInteger(double x)
Returns
true if
x represents a mathematical integer.
|
static boolean |
isPowerOfTwo(double x)
Returns
true if
x is exactly equal to
2^k for some finite integer
k.
|
static double |
log2(double x)
Returns the base 2 logarithm of a double value.
|
static int |
log2(double x, RoundingMode
Returns the base 2 logarithm of a double value, rounded with the specified rounding mode to an
int.
|
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(Iterable
Returns the arithmetic mean of the values.
|
static double |
mean(Iterator
Returns the arithmetic mean of the values.
|
static double |
mean(long... values)
Returns the arithmetic mean of the values.
|
static BigInteger |
roundToBigInteger(double x, RoundingMode
Returns the
BigInteger value that is equal to
x rounded with the specified rounding mode, if possible.
|
static int |
roundToInt(double x, RoundingMode
Returns the
int value that is equal to
x rounded with the specified rounding mode, if possible.
|
static long |
roundToLong(double x, RoundingMode
Returns the
long value that is equal to
x rounded with the specified rounding mode, if possible.
|
@GwtIncompatible(value="#roundIntermediate") public static int roundToInt(double x, RoundingModemode)
int value that is equal to
x rounded with the specified rounding mode, if possible.
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 @GwtIncompatible(value="#roundIntermediate") public static long roundToLong(double x, RoundingModemode)
long value that is equal to
x rounded with the specified rounding mode, if possible.
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 @GwtIncompatible(value="#roundIntermediate, java.lang.Math.getExponent, com.google.common.math.DoubleUtils") public static BigIntegerroundToBigInteger(double x, RoundingMode mode)
BigInteger value that is equal to
x rounded with the specified rounding mode, if possible.
ArithmeticException - if
x is infinite or NaN x is not a mathematical integer and mode is RoundingMode.UNNECESSARY @GwtIncompatible(value="com.google.common.math.DoubleUtils") public static boolean isPowerOfTwo(double x)
true if
x is exactly equal to
2^k for some finite integer
k.
public 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.
@GwtIncompatible(value="java.lang.Math.getExponent, com.google.common.math.DoubleUtils") public static int log2(double x, RoundingModemode)
int.
Regardless of the rounding mode, this is faster than (int) log2(x).
IllegalArgumentException - if
x <= 0.0,
x is NaN, or
x is infinite
@GwtIncompatible(value="java.lang.Math.getExponent, com.google.common.math.DoubleUtils") public 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).
public static double factorial(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.
IllegalArgumentException - if
n < 0
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 implementations.
IllegalArgumentException - if
tolerance is
< 0 or NaN
public 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, it treats all NaN values as equal and greater than all other values (including Double).
This is not a total ordering and is not suitable for use in Comparable implementations. In particular, it is not transitive.
IllegalArgumentException - if
tolerance is
< 0 or NaN
@GwtIncompatible(value="MeanAccumulator") public static double mean(double... values)
@GwtIncompatible(value="MeanAccumulator") public static double mean(int... values)
@GwtIncompatible(value="MeanAccumulator") public static double mean(long... values)
@GwtIncompatible(value="MeanAccumulator") public static double mean(Iterable<? extends Number > values)
@GwtIncompatible(value="MeanAccumulator") public static double mean(Iterator<? extends Number > values)