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); } } }
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)); } }
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)); } } } }
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); } }
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); } }
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)); } }
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) { } } } }
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) { } } } }
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); } } }
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))); }
public void testLog2Zero() { assertEquals(Double.NEGATIVE_INFINITY, DoubleMath.log2(0.0)); assertEquals(Double.NEGATIVE_INFINITY, DoubleMath.log2(-0.0)); }
public void testLog2Negative() { for (double d : POSITIVE_FINITE_DOUBLE_CANDIDATES) { assertTrue(Double.isNaN(DoubleMath.log2(-d))); } }
public void testLog2SemiMonotonic() { for (double d : POSITIVE_FINITE_DOUBLE_CANDIDATES) { assertTrue(DoubleMath.log2(d + 0.01) >= DoubleMath.log2(d)); } }