Top Description Fields Constructors Methods
com.google.common.math

public final Class DoubleMath

extends Object
Class Inheritance
Annotations
@GwtCompatible
emulated:true
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, com.google.common.math.DoubleUtils.IMPLICIT_BIT, .DoubleUtils.SIGNIFICAND_BITS, .DoubleUtils.getSignificand, .DoubleUtils.isFinite, .DoubleUtils.isNormal, .DoubleUtils.scaleNormalize, .MathPreconditions.checkInRangeForRoundingInputs, .MathPreconditions.checkNonNegative, .MathPreconditions.checkRoundingUnnecessary, java.lang.Math.abs, .Math.copySign, .Math.getExponent, .Math.log, .Math.rint

A class for arithmetic on doubles that is not covered by java.lang.Math.
Author
Louis Wasserman
Since
11.0

Field Summary

Modifier and TypeField and Description
pack-priv static final double[]
private static final double
pack-priv static final int
private static final double
private static final double
private static final double
private static final double

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static double
checkFinite(double argument)

public static double
factorial(int n)

Returns n!

public static int
fuzzyCompare(double a, double b, double tolerance)

Compares a and b "fuzzily," with a tolerance for nearly-equal values.

public static boolean
fuzzyEquals(double a, double b, double tolerance)

Returns true if a and b are within tolerance of each other.

public static boolean
isMathematicalInteger(double x)

Returns true if x represents a mathematical integer.

public static boolean
isPowerOfTwo(double x)

Returns true if x is exactly equal to 2^k for some finite integer k.

public static double
log2(double x)

Returns the base 2 logarithm of a double value.

public static int
log2(double x, RoundingMode mode)

Returns the base 2 logarithm of a double value, rounded with the specified rounding mode to an int.

public static double
mean(double...
a nonempty series of values
values
)

Deprecated Use Stats#meanOf instead, noting the less strict handling of non-finite values.
Returns the arithmetic mean of values.
public static double
mean(int...
a nonempty series of values
values
)

Deprecated Use Stats#meanOf instead, noting the less strict handling of non-finite values.
Returns the arithmetic mean of values.
public static double
mean(long...
a nonempty series of values, which will be converted to double values (this may cause loss of precision for longs of magnitude over 2^53 (slightly over 9e15))
values
)

Deprecated Use Stats#meanOf instead, noting the less strict handling of non-finite values.
Returns the arithmetic mean of values.
public static double
mean(Iterable<? extends Number>
a nonempty series of values, which will be converted to double values (this may cause loss of precision)
values
)

Deprecated Use Stats#meanOf instead, noting the less strict handling of non-finite values.
Returns the arithmetic mean of values.
public static double
mean(Iterator<? extends Number>
a nonempty series of values, which will be converted to double values (this may cause loss of precision)
values
)

Deprecated Use Stats#meanOf instead, noting the less strict handling of non-finite values.
Returns the arithmetic mean of values.
pack-priv static double
public static BigInteger
roundToBigInteger(double x, RoundingMode mode)

Returns the BigInteger value that is equal to x rounded with the specified rounding mode, if possible.

public static int
roundToInt(double x, RoundingMode mode)

Returns the int value that is equal to x rounded with the specified rounding mode, if possible.

public static long
roundToLong(double x, RoundingMode mode)

Returns the long value that is equal to x rounded with the specified rounding mode, if possible.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

everySixteenthFactorialback to summary
pack-priv static final double[] everySixteenthFactorial
Annotations
@VisibleForTesting
LN_2back to summary
private static final double LN_2
MAX_FACTORIALback to summary
pack-priv static final int MAX_FACTORIAL
Annotations
@VisibleForTesting
MAX_INT_AS_DOUBLEback to summary
private static final double MAX_INT_AS_DOUBLE
MAX_LONG_AS_DOUBLE_PLUS_ONEback to summary
private static final double MAX_LONG_AS_DOUBLE_PLUS_ONE
MIN_INT_AS_DOUBLEback to summary
private static final double MIN_INT_AS_DOUBLE
MIN_LONG_AS_DOUBLEback to summary
private static final double MIN_LONG_AS_DOUBLE

Constructor Detail

DoubleMathback to summary
private DoubleMath()

Method Detail

checkFiniteback to summary
private static double checkFinite(double argument)
Annotations
@J2ktIncompatible
@GwtIncompatible
@CanIgnoreReturnValue
factorialback to summary
public 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#POSITIVE_INFINITY if n! > Double.MAX_VALUE.

The result is within 1 ulp of the true value.

Exceptions
IllegalArgumentException:
if n < 0
fuzzyCompareback to summary
public static int fuzzyCompare(double a, double b, double tolerance)

Compares 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#compare(double, double), it treats all NaN values as equal and greater than all other values (including Double#POSITIVE_INFINITY).

This is not a total ordering and is not suitable for use in Comparable#compareTo implementations. In particular, it is not transitive.

Exceptions
IllegalArgumentException:
if tolerance is < 0 or NaN
Since
13.0
fuzzyEqualsback to summary
public static boolean fuzzyEquals(double a, double b, double tolerance)

Returns 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:

  • All NaNs are fuzzily equal.
  • If a == b, then a and b are always fuzzily equal.
  • Positive and negative zero are always fuzzily equal.
  • If tolerance is zero, and neither a nor b is NaN, then a and b are fuzzily equal if and only if a == b.
  • With Double#POSITIVE_INFINITY tolerance, all non-NaN values are fuzzily equal.
  • With finite tolerance, 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#equals implementations.

Exceptions
IllegalArgumentException:
if tolerance is < 0 or NaN
Since
13.0
isMathematicalIntegerback to summary
public static boolean isMathematicalInteger(double x)

Returns 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).

Annotations
@GwtIncompatible
isPowerOfTwoback to summary
public static boolean isPowerOfTwo(double x)

Returns true if x is exactly equal to 2^k for some finite integer k.

Annotations
@J2ktIncompatible
@GwtIncompatible
log2back to summary
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.

log2back to summary
public static int log2(double x, RoundingMode mode)

Returns the base 2 logarithm of a double value, rounded with the specified rounding mode to an int.

Regardless of the rounding mode, this is faster than (int) log2(x).

Annotations
@J2ktIncompatible
@GwtIncompatible
@SuppressWarnings:fallthrough
Exceptions
IllegalArgumentException:
if x <= 0.0, x is NaN, or x is infinite
meanback to summary
public static double mean(double... values)

Deprecated

Use Stats#meanOf instead, noting the less strict handling of non-finite values.

Returns the arithmetic mean of values.

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:double[]

a nonempty series of values

Annotations
@Deprecated
@J2ktIncompatible
@GwtIncompatible
Exceptions
IllegalArgumentException:
if values is empty or contains any non-finite value
meanback to summary
public static double mean(int... values)

Deprecated

Use Stats#meanOf instead, noting the less strict handling of non-finite values.

Returns the arithmetic mean of values.

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:int[]

a nonempty series of values

Annotations
@Deprecated
Exceptions
IllegalArgumentException:
if values is empty
meanback to summary
public static double mean(long... values)

Deprecated

Use Stats#meanOf instead, noting the less strict handling of non-finite values.

Returns the arithmetic mean of values.

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:long[]

a nonempty series of values, which will be converted to double values (this may cause loss of precision for longs of magnitude over 2^53 (slightly over 9e15))

Annotations
@Deprecated
Exceptions
IllegalArgumentException:
if values is empty
meanback to summary
public static double mean(Iterable<? extends Number> values)

Deprecated

Use Stats#meanOf instead, noting the less strict handling of non-finite values.

Returns the arithmetic mean of values.

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:Iterable<? extends Number>

a nonempty series of values, which will be converted to double values (this may cause loss of precision)

Annotations
@Deprecated
@J2ktIncompatible
@GwtIncompatible
Exceptions
IllegalArgumentException:
if values is empty or contains any non-finite value
meanback to summary
public static double mean(Iterator<? extends Number> values)

Deprecated

Use Stats#meanOf instead, noting the less strict handling of non-finite values.

Returns the arithmetic mean of values.

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:Iterator<? extends Number>

a nonempty series of values, which will be converted to double values (this may cause loss of precision)

Annotations
@Deprecated
@J2ktIncompatible
@GwtIncompatible
Exceptions
IllegalArgumentException:
if values is empty or contains any non-finite value
roundIntermediateback to summary
pack-priv static double roundIntermediate(double x, RoundingMode mode)
Annotations
@J2ktIncompatible
@GwtIncompatible
roundToBigIntegerback to summary
public static BigInteger roundToBigInteger(double x, RoundingMode mode)

Returns the BigInteger value that is equal to x rounded with the specified rounding mode, if possible.

Annotations
@J2ktIncompatible
@GwtIncompatible
Exceptions
ArithmeticException:
if
roundToIntback to summary
public static int roundToInt(double x, RoundingMode mode)

Returns the int value that is equal to x rounded with the specified rounding mode, if possible.

Annotations
@J2ktIncompatible
@GwtIncompatible
Exceptions
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
roundToLongback to summary
public static long roundToLong(double x, RoundingMode mode)

Returns the long value that is equal to x rounded with the specified rounding mode, if possible.

Annotations
@J2ktIncompatible
@GwtIncompatible
Exceptions
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