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 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)); } }
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 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)); } } } }
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); } } }