Class DoubleMath
- java.lang.Object
-
- com.google.common.math.DoubleMath
-
@GwtCompatible(emulated=true) public final class DoubleMath extends java.lang.Object
A class for arithmetic on doubles that is not covered byMath.- Since:
- 11.0
- Author:
- Louis Wasserman
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static doublefactorial(int n)Returnsn!, that is, the product of the firstnpositive integers,1ifn == 0, orn!, orDouble.POSITIVE_INFINITYifn! > Double.MAX_VALUE.static intfuzzyCompare(double a, double b, double tolerance)Comparesaandb"fuzzily," with a tolerance for nearly-equal values.static booleanfuzzyEquals(double a, double b, double tolerance)Returnstrueifaandbare withintoleranceof each other.static booleanisMathematicalInteger(double x)Returnstrueifxrepresents a mathematical integer.static booleanisPowerOfTwo(double x)Returnstrueifxis exactly equal to2^kfor some finite integerk.static doublelog2(double x)Returns the base 2 logarithm of a double value.static intlog2(double x, java.math.RoundingMode mode)Returns the base 2 logarithm of a double value, rounded with the specified rounding mode to anint.static doublemean(double... values)Deprecated.UseStats.meanOf(java.lang.Iterable<? extends java.lang.Number>)instead, noting the less strict handling of non-finite values.static doublemean(int... values)Deprecated.UseStats.meanOf(java.lang.Iterable<? extends java.lang.Number>)instead, noting the less strict handling of non-finite values.static doublemean(long... values)Deprecated.UseStats.meanOf(java.lang.Iterable<? extends java.lang.Number>)instead, noting the less strict handling of non-finite values.static doublemean(java.lang.Iterable<? extends java.lang.Number> values)Deprecated.UseStats.meanOf(java.lang.Iterable<? extends java.lang.Number>)instead, noting the less strict handling of non-finite values.static doublemean(java.util.Iterator<? extends java.lang.Number> values)Deprecated.UseStats.meanOf(java.lang.Iterable<? extends java.lang.Number>)instead, noting the less strict handling of non-finite values.static java.math.BigIntegerroundToBigInteger(double x, java.math.RoundingMode mode)Returns theBigIntegervalue that is equal toxrounded with the specified rounding mode, if possible.static introundToInt(double x, java.math.RoundingMode mode)Returns theintvalue that is equal toxrounded with the specified rounding mode, if possible.static longroundToLong(double x, java.math.RoundingMode mode)Returns thelongvalue that is equal toxrounded with the specified rounding mode, if possible.
-
-
-
Method Detail
-
roundToInt
@GwtIncompatible public static int roundToInt(double x, java.math.RoundingMode mode)
Returns theintvalue that is equal toxrounded with the specified rounding mode, if possible.- Throws:
java.lang.ArithmeticException- ifxis infinite or NaNx, after being rounded to a mathematical integer using the specified rounding mode, is either less thanInteger.MIN_VALUEor greater thanInteger.MAX_VALUExis not a mathematical integer andmodeisRoundingMode.UNNECESSARY
-
roundToLong
@GwtIncompatible public static long roundToLong(double x, java.math.RoundingMode mode)
Returns thelongvalue that is equal toxrounded with the specified rounding mode, if possible.- Throws:
java.lang.ArithmeticException- ifxis infinite or NaNx, after being rounded to a mathematical integer using the specified rounding mode, is either less thanLong.MIN_VALUEor greater thanLong.MAX_VALUExis not a mathematical integer andmodeisRoundingMode.UNNECESSARY
-
roundToBigInteger
@GwtIncompatible public static java.math.BigInteger roundToBigInteger(double x, java.math.RoundingMode mode)
Returns theBigIntegervalue that is equal toxrounded with the specified rounding mode, if possible.- Throws:
java.lang.ArithmeticException- ifxis infinite or NaNxis not a mathematical integer andmodeisRoundingMode.UNNECESSARY
-
isPowerOfTwo
@GwtIncompatible public static boolean isPowerOfTwo(double x)
Returnstrueifxis exactly equal to2^kfor some finite integerk.
-
log2
public static double log2(double x)
Returns the base 2 logarithm of a double value.Special cases:
- If
xis NaN or less than zero, the result is NaN. - If
xis positive infinity, the result is positive infinity. - If
xis 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
@GwtIncompatible 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,xis NaN, orxis infinite
-
isMathematicalInteger
@GwtIncompatible public static boolean isMathematicalInteger(double x)
Returnstrueifxrepresents 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 firstnpositive integers,1ifn == 0, orn!, orDouble.POSITIVE_INFINITYifn! > 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)
Returnstrueifaandbare withintoleranceof 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, thenaandbare always fuzzily equal. - Positive and negative zero are always fuzzily equal.
- If
toleranceis zero, and neitheranorbis NaN, thenaandbare fuzzily equal if and only ifa == b. - With
Double.POSITIVE_INFINITYtolerance, all non-NaN values are fuzzily equal. - With finite tolerance,
Double.POSITIVE_INFINITYandDouble.NEGATIVE_INFINITYare 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- iftoleranceis< 0or NaN- Since:
- 13.0
-
fuzzyCompare
public static int fuzzyCompare(double a, double b, double tolerance)
Comparesaandb"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- iftoleranceis< 0or NaN- Since:
- 13.0
-
mean
@Deprecated @GwtIncompatible public static double mean(double... values)
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:
java.lang.IllegalArgumentException- ifvaluesis empty or contains any non-finite value
-
mean
@Deprecated public 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.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:
java.lang.IllegalArgumentException- ifvaluesis empty
-
mean
@Deprecated public 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.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 todoublevalues (this may cause loss of precision for longs of magnitude over 2^53 (slightly over 9e15))- Throws:
java.lang.IllegalArgumentException- ifvaluesis empty
-
mean
@Deprecated @GwtIncompatible public static double mean(java.lang.Iterable<? extends java.lang.Number> values)
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 todoublevalues (this may cause loss of precision)- Throws:
java.lang.IllegalArgumentException- ifvaluesis empty or contains any non-finite value
-
mean
@Deprecated @GwtIncompatible public static double mean(java.util.Iterator<? extends java.lang.Number> values)
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 todoublevalues (this may cause loss of precision)- Throws:
java.lang.IllegalArgumentException- ifvaluesis empty or contains any non-finite value
-
-