Exemplo n.º 1
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));
   }
 }
Exemplo n.º 2
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));
   }
 }
Exemplo n.º 3
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));
       }
     }
   }
 }
Exemplo n.º 4
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));
       }
     }
   }
 }
Exemplo n.º 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);
     }
   }
 }