Exemple #1
0
 public void testFuzzyEqualsOneNaN() {
   for (double a : DOUBLE_CANDIDATES_EXCEPT_NAN) {
     for (double tolerance : TOLERANCE_CANDIDATES) {
       assertFalse(DoubleMath.fuzzyEquals(a, Double.NaN, tolerance));
       assertFalse(DoubleMath.fuzzyEquals(Double.NaN, a, tolerance));
     }
   }
 }
Exemple #2
0
 public void testFuzzyInfiniteVersusInfiniteWithFiniteTolerance() {
   for (double inf : INFINITIES) {
     for (double tolerance : FINITE_TOLERANCE_CANDIDATES) {
       assertTrue(DoubleMath.fuzzyEquals(inf, inf, tolerance));
       assertFalse(DoubleMath.fuzzyEquals(inf, -inf, tolerance));
     }
   }
 }
Exemple #3
0
 public void testIsPowerOfTwo() {
   for (double x : ALL_DOUBLE_CANDIDATES) {
     boolean expected =
         x > 0
             && !Double.isInfinite(x)
             && !Double.isNaN(x)
             && StrictMath.pow(2.0, DoubleMath.log2(x, FLOOR)) == x;
     assertEquals(expected, DoubleMath.isPowerOfTwo(x));
   }
 }
Exemple #4
0
 public void testFuzzyCompare() {
   for (double a : ALL_DOUBLE_CANDIDATES) {
     for (double b : ALL_DOUBLE_CANDIDATES) {
       for (double tolerance : TOLERANCE_CANDIDATES) {
         int expected = DoubleMath.fuzzyEquals(a, b, tolerance) ? 0 : Double.compare(a, b);
         int actual = DoubleMath.fuzzyCompare(a, b, tolerance);
         assertEquals(Integer.signum(expected), Integer.signum(actual));
       }
     }
   }
 }
Exemple #5
0
 public void testRoundLog2Exact() {
   for (double x : POSITIVE_FINITE_DOUBLE_CANDIDATES) {
     boolean isPowerOfTwo = StrictMath.pow(2.0, DoubleMath.log2(x, FLOOR)) == x;
     try {
       int log2 = DoubleMath.log2(x, UNNECESSARY);
       assertEquals(x, Math.scalb(1.0, log2));
       assertTrue(isPowerOfTwo);
     } catch (ArithmeticException e) {
       assertFalse(isPowerOfTwo);
     }
   }
 }
Exemple #6
0
 public void testRoundInfiniteToBigIntegerAlwaysFails() {
   for (RoundingMode mode : ALL_ROUNDING_MODES) {
     try {
       DoubleMath.roundToBigInteger(Double.POSITIVE_INFINITY, mode);
       fail("Expected ArithmeticException");
     } catch (ArithmeticException expected) {
     }
     try {
       DoubleMath.roundToBigInteger(Double.NEGATIVE_INFINITY, mode);
       fail("Expected ArithmeticException");
     } catch (ArithmeticException expected) {
     }
   }
 }
Exemple #7
0
 public void testFactorial() {
   for (int i = 0; i <= DoubleMath.MAX_FACTORIAL; i++) {
     double actual = BigIntegerMath.factorial(i).doubleValue();
     double result = DoubleMath.factorial(i);
     assertEquals(actual, result, Math.ulp(actual));
   }
 }
Exemple #8
0
 public void testLog2Accuracy() {
   for (double d : POSITIVE_FINITE_DOUBLE_CANDIDATES) {
     double dmLog2 = DoubleMath.log2(d);
     double trueLog2 = trueLog2(d);
     assertTrue(Math.abs(dmLog2 - trueLog2) <= Math.ulp(trueLog2));
   }
 }
Exemple #9
0
 public void testFuzzyEqualsInfiniteTolerance() {
   for (double a : DOUBLE_CANDIDATES_EXCEPT_NAN) {
     for (double b : DOUBLE_CANDIDATES_EXCEPT_NAN) {
       assertTrue(DoubleMath.fuzzyEquals(a, b, Double.POSITIVE_INFINITY));
     }
   }
 }
Exemple #10
0
 public void testRoundLog2Half() {
   // We don't expect perfect rounding accuracy.
   for (int exp : asList(-1022, -50, -1, 0, 1, 2, 3, 4, 100, 1022, 1023)) {
     for (RoundingMode mode : asList(HALF_EVEN, HALF_UP, HALF_DOWN)) {
       double x = Math.scalb(Math.sqrt(2) + 0.001, exp);
       double y = Math.scalb(Math.sqrt(2) - 0.001, exp);
       if (exp < 0) {
         assertEquals(exp + 1, DoubleMath.log2(x, mode));
         assertEquals(exp, DoubleMath.log2(y, mode));
       } else {
         assertEquals(exp + 1, DoubleMath.log2(x, mode));
         assertEquals(exp, DoubleMath.log2(y, mode));
       }
     }
   }
 }
Exemple #11
0
 public void testRoundLog2Floor() {
   for (double d : POSITIVE_FINITE_DOUBLE_CANDIDATES) {
     int log2 = DoubleMath.log2(d, FLOOR);
     assertTrue(StrictMath.pow(2.0, log2) <= d);
     assertTrue(StrictMath.pow(2.0, log2 + 1) > d);
   }
 }
Exemple #12
0
 public void testRoundFractionalDoubleToBigInteger() {
   for (double d : FRACTIONAL_DOUBLE_CANDIDATES) {
     for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) {
       BigDecimal expected = new BigDecimal(d).setScale(0, mode);
       assertEquals(expected.toBigInteger(), DoubleMath.roundToBigInteger(d, mode));
     }
   }
 }
Exemple #13
0
 public void testRoundLog2Ceiling() {
   for (double d : POSITIVE_FINITE_DOUBLE_CANDIDATES) {
     int log2 = DoubleMath.log2(d, CEILING);
     assertTrue(StrictMath.pow(2.0, log2) >= d);
     double z = StrictMath.pow(2.0, log2 - 1);
     assertTrue(z < d);
   }
 }
Exemple #14
0
 public void testRoundExactFractionalDoubleToBigIntegerFails() {
   for (double d : FRACTIONAL_DOUBLE_CANDIDATES) {
     try {
       DoubleMath.roundToBigInteger(d, UNNECESSARY);
       fail("Expected ArithmeticException");
     } catch (ArithmeticException expected) {
     }
   }
 }
Exemple #15
0
 public void testRoundNaNToBigIntegerAlwaysFails() {
   for (RoundingMode mode : ALL_ROUNDING_MODES) {
     try {
       DoubleMath.roundToBigInteger(Double.NaN, mode);
       fail("Expected ArithmeticException");
     } catch (ArithmeticException expected) {
     }
   }
 }
Exemple #16
0
 public void testFactorialNegative() {
   for (int n : NEGATIVE_INTEGER_CANDIDATES) {
     try {
       DoubleMath.factorial(n);
       fail("Expected IllegalArgumentException");
     } catch (IllegalArgumentException expected) {
     }
   }
 }
Exemple #17
0
 public void testFuzzyEqualsFinite() {
   for (double a : FINITE_DOUBLE_CANDIDATES) {
     for (double b : FINITE_DOUBLE_CANDIDATES) {
       for (double tolerance : FINITE_TOLERANCE_CANDIDATES) {
         assertEquals(Math.abs(a - b) <= tolerance, DoubleMath.fuzzyEquals(a, b, tolerance));
       }
     }
   }
 }
Exemple #18
0
 public void testFuzzyCompareBadTolerance() {
   for (double tolerance : BAD_TOLERANCE_CANDIDATES) {
     try {
       DoubleMath.fuzzyCompare(1, 2, tolerance);
       fail("Expected IllegalArgumentException");
     } catch (IllegalArgumentException expected) {
       // success
     }
   }
 }
Exemple #19
0
 public void testFuzzyEqualsZeroTolerance() {
   // make sure we test -0 tolerance
   for (double zero : Doubles.asList(0.0, -0.0)) {
     for (double a : ALL_DOUBLE_CANDIDATES) {
       for (double b : ALL_DOUBLE_CANDIDATES) {
         assertEquals(
             a == b || (Double.isNaN(a) && Double.isNaN(b)), DoubleMath.fuzzyEquals(a, b, zero));
       }
     }
   }
 }
Exemple #20
0
 public void testRoundLog2ThrowsOnNegative() {
   for (RoundingMode mode : ALL_ROUNDING_MODES) {
     for (double d : POSITIVE_FINITE_DOUBLE_CANDIDATES) {
       try {
         DoubleMath.log2(-d, mode);
         fail("Expected IllegalArgumentException");
       } catch (IllegalArgumentException e) {
       }
     }
   }
 }
Exemple #21
0
 public void testRoundLog2ThrowsOnZerosInfinitiesAndNaN() {
   for (RoundingMode mode : ALL_ROUNDING_MODES) {
     for (double d :
         asList(0.0, -0.0, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN)) {
       try {
         DoubleMath.log2(d, mode);
         fail("Expected IllegalArgumentException");
       } catch (IllegalArgumentException e) {
       }
     }
   }
 }
Exemple #22
0
 public void testRoundLog2Up() {
   for (double d : POSITIVE_FINITE_DOUBLE_CANDIDATES) {
     int log2 = DoubleMath.log2(d, UP);
     if (d >= 1.0) {
       assertTrue(log2 >= 0);
       assertTrue(StrictMath.pow(2.0, log2) >= d);
       assertTrue(StrictMath.pow(2.0, log2 - 1) < d);
     } else {
       assertTrue(log2 <= 0);
       assertTrue(StrictMath.pow(2.0, log2) <= d);
       assertTrue(StrictMath.pow(2.0, log2 + 1) > d);
     }
   }
 }
Exemple #23
0
  public void testRoundExactIntegralDoubleToInt() {
    for (double d : INTEGRAL_DOUBLE_CANDIDATES) {
      BigDecimal expected = new BigDecimal(d).setScale(0, UNNECESSARY);
      boolean isInBounds =
          expected.compareTo(MAX_INT_AS_BIG_DECIMAL) <= 0
              & expected.compareTo(MIN_INT_AS_BIG_DECIMAL) >= 0;

      try {
        assertEquals(expected.intValue(), DoubleMath.roundToInt(d, UNNECESSARY));
        assertTrue(isInBounds);
      } catch (ArithmeticException e) {
        assertFalse(isInBounds);
      }
    }
  }
Exemple #24
0
  public void testRoundFractionalDoubleToInt() {
    for (double d : FRACTIONAL_DOUBLE_CANDIDATES) {
      for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) {
        BigDecimal expected = new BigDecimal(d).setScale(0, mode);
        boolean isInBounds =
            expected.compareTo(MAX_INT_AS_BIG_DECIMAL) <= 0
                & expected.compareTo(MIN_INT_AS_BIG_DECIMAL) >= 0;

        try {
          assertEquals(expected.intValue(), DoubleMath.roundToInt(d, mode));
          assertTrue(isInBounds);
        } catch (ArithmeticException e) {
          assertFalse(isInBounds);
        }
      }
    }
  }
Exemple #25
0
 public void testIsMathematicalIntegerIntegral() {
   for (double d : INTEGRAL_DOUBLE_CANDIDATES) {
     assertTrue(DoubleMath.isMathematicalInteger(d));
   }
 }
Exemple #26
0
 public void testLog2NaNInfinity() {
   assertEquals(Double.POSITIVE_INFINITY, DoubleMath.log2(Double.POSITIVE_INFINITY));
   assertTrue(Double.isNaN(DoubleMath.log2(Double.NEGATIVE_INFINITY)));
   assertTrue(Double.isNaN(DoubleMath.log2(Double.NaN)));
 }
Exemple #27
0
 public void testIsMathematicalIntegerFractional() {
   for (double d : FRACTIONAL_DOUBLE_CANDIDATES) {
     assertFalse(DoubleMath.isMathematicalInteger(d));
   }
 }
Exemple #28
0
 public void testIsMathematicalIntegerNotFinite() {
   for (double d : Arrays.asList(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN)) {
     assertFalse(DoubleMath.isMathematicalInteger(d));
   }
 }
Exemple #29
0
 public void testFuzzyEqualsTwoNaNs() {
   for (double tolerance : TOLERANCE_CANDIDATES) {
     assertTrue(DoubleMath.fuzzyEquals(Double.NaN, Double.NaN, tolerance));
   }
 }
Exemple #30
0
 public void testFactorialTooHigh() {
   assertEquals(Double.POSITIVE_INFINITY, DoubleMath.factorial(DoubleMath.MAX_FACTORIAL + 1));
   assertEquals(Double.POSITIVE_INFINITY, DoubleMath.factorial(DoubleMath.MAX_FACTORIAL + 20));
 }