Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (C) 2011 The Guava Authors
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 import static;
 import static java.lang.Math.log;
A class for arithmetic on doubles that is not covered by java.lang.Math.

Louis Wasserman
 @GwtCompatible(emulated = true)
 public final class DoubleMath {
    * This method returns a value y such that rounding y DOWN (towards zero) gives the same result
    * as rounding x according to the specified mode.
   private static final double MIN_INT_AS_DOUBLE = -0x1p31;
   private static final double MAX_INT_AS_DOUBLE = 0x1p31 - 1.0;
   private static final double MIN_LONG_AS_DOUBLE = -0x1p63;
    * We cannot store Long.MAX_VALUE as a double without losing precision.  Instead, we store
    * Long.MAX_VALUE + 1 == -Long.MIN_VALUE, and then offset all comparisons by 1.
   private static final double MAX_LONG_AS_DOUBLE_PLUS_ONE = 0x1p63;

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.

   public static double log2(double x) {
     return log(x) / // surprisingly within 1 ulp according to tests
   private static final double LN_2 = log(2);

Returns n!, that is, the product of the first n positive integers, 1 if n == 0, or e n!}, or java.lang.Double.POSITIVE_INFINITY if n! > Double.MAX_VALUE.

The result is within 1 ulp of the true value.

   public static double factorial(int n) {
     if (n > ) {
       return .;
     } else {
       // Multiplying the last (n & 0xf) values into their own accumulator gives a more accurate
       // result than multiplying by everySixteenthFactorial[n >> 4] directly.
       double accum = 1.0;
       for (int i = 1 + (n & ~0xf); i <= ni++) {
         accum *= i;
       return accum * [n >> 4];
   static final int MAX_FACTORIAL = 170;
   static final double[] everySixteenthFactorial = {

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 java.lang.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 java.lang.Object.equals(java.lang.Object) implementations.

    java.lang.IllegalArgumentException if tolerance is < 0 or NaN
  public static boolean fuzzyEquals(double adouble bdouble tolerance) {
          Math.copySign(a - b, 1.0) <= tolerance
           // copySign(x, 1.0) is a branch-free version of abs(x), but with different NaN semantics
          || (a == b// needed to ensure that infinities equal themselves
          || (Double.isNaN(a) && Double.isNaN(b));

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

This method is equivalent to fuzzyEquals(a, b, tolerance) ? 0 :, b). In particular, like,double), it treats all NaN values as equal and greater than all other values (including java.lang.Double.POSITIVE_INFINITY).

This is not a total ordering and is not suitable for use in java.lang.Comparable.compareTo(java.lang.Object) implementations. In particular, it is not transitive.

java.lang.IllegalArgumentException if tolerance is < 0 or NaN
  public static int fuzzyCompare(double adouble bdouble tolerance) {
    if (fuzzyEquals(abtolerance)) {
      return 0;
    } else if (a < b) {
      return -1;
    } else if (a > b) {
      return 1;
    } else {
      return, Double.isNaN(b));
  private DoubleMath() {}
New to GrepCode? Check out our FAQ X