Esempio n. 1
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);
   }
 }
Esempio n. 2
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);
   }
 }
Esempio n. 3
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);
     }
   }
 }
Esempio n. 4
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));
   }
 }
Esempio 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);
     }
   }
 }
Esempio n. 6
0
 private strictfp double trueLog2(double d) {
   double trueLog2 = StrictMath.log(d) / StrictMath.log(2);
   // increment until it's >= the true value
   while (StrictMath.pow(2.0, trueLog2) < d) {
     trueLog2 = StrictMath.nextUp(trueLog2);
   }
   // decrement until it's <= the true value
   while (StrictMath.pow(2.0, trueLog2) > d) {
     trueLog2 = StrictMath.nextAfter(trueLog2, Double.NEGATIVE_INFINITY);
   }
   if (StrictMath.abs(StrictMath.pow(2.0, trueLog2) - d)
       > StrictMath.abs(StrictMath.pow(2.0, StrictMath.nextUp(trueLog2)) - d)) {
     trueLog2 = StrictMath.nextUp(trueLog2);
   }
   return trueLog2;
 }
Esempio n. 7
0
 public void testIsPowerOfTwoYes() {
   for (int i = -1074; i <= 1023; i++) {
     assertTrue(DoubleMath.isPowerOfTwo(StrictMath.pow(2.0, i)));
   }
 }