Class DoubleMath
Math.- Since:
- 11.0
- Author:
- Louis Wasserman
- 
Method SummaryModifier and TypeMethodDescriptionstatic 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, RoundingMode mode) Returns the base 2 logarithm of a double value, rounded with the specified rounding mode to anint.static doublemean(double... values) Deprecated.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 doubleDeprecated.UseStats.meanOf(java.lang.Iterable<? extends java.lang.Number>)instead, noting the less strict handling of non-finite values.static doubleDeprecated.UseStats.meanOf(java.lang.Iterable<? extends java.lang.Number>)instead, noting the less strict handling of non-finite values.static BigIntegerroundToBigInteger(double x, RoundingMode mode) Returns theBigIntegervalue that is equal toxrounded with the specified rounding mode, if possible.static introundToInt(double x, RoundingMode mode) Returns theintvalue that is equal toxrounded with the specified rounding mode, if possible.static longroundToLong(double x, RoundingMode mode) Returns thelongvalue that is equal toxrounded with the specified rounding mode, if possible.
- 
Method Details- 
roundToIntReturns theintvalue that is equal toxrounded with the specified rounding mode, if possible.- Throws:
- ArithmeticException- if- xis infinite or NaN
- x, after being rounded to a mathematical integer using the specified rounding mode, is either less than- Integer.MIN_VALUEor greater than- Integer.MAX_VALUE
- xis not a mathematical integer and- modeis- RoundingMode.UNNECESSARY
 
 
- 
roundToLongReturns thelongvalue that is equal toxrounded with the specified rounding mode, if possible.- Throws:
- ArithmeticException- if- xis infinite or NaN
- x, after being rounded to a mathematical integer using the specified rounding mode, is either less than- Long.MIN_VALUEor greater than- Long.MAX_VALUE
- xis not a mathematical integer and- modeis- RoundingMode.UNNECESSARY
 
 
- 
roundToBigIntegerReturns theBigIntegervalue that is equal toxrounded with the specified rounding mode, if possible.- Throws:
- ArithmeticException- if- xis infinite or NaN
- xis not a mathematical integer and- modeis- RoundingMode.UNNECESSARY
 
 
- 
isPowerOfTwoReturnstrueifxis exactly equal to2^kfor some finite integerk.
- 
log2public 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 
- 
log2Returns 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- if- x <= 0.0,- xis NaN, or- xis infinite
 
- 
isMathematicalIntegerReturnstrueifxrepresents a mathematical integer.This is equivalent to, but not necessarily implemented as, the expression !Double.isNaN(x) && !Double.isInfinite(x) && x == Math.rint(x).
- 
factorialpublic 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:
- IllegalArgumentException- if- n < 0
 
- 
fuzzyEqualspublic 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:
- IllegalArgumentException- if- toleranceis- < 0or NaN
- Since:
- 13.0
 
- 
fuzzyComparepublic 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:
- IllegalArgumentException- if- toleranceis- < 0or NaN
- Since:
- 13.0
 
- 
meanDeprecated.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- if- valuesis empty or contains any non-finite value
 
- 
meanDeprecated.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- if- valuesis empty
 
- 
meanDeprecated.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 to- doublevalues (this may cause loss of precision for longs of magnitude over 2^53 (slightly over 9e15))
- Throws:
- IllegalArgumentException- if- valuesis empty
 
- 
meanDeprecated.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 to- doublevalues (this may cause loss of precision)
- Throws:
- IllegalArgumentException- if- valuesis empty or contains any non-finite value
 
- 
meanDeprecated.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 to- doublevalues (this may cause loss of precision)
- Throws:
- IllegalArgumentException- if- valuesis 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.