@Test public void testHypotenuse() throws Exception { Random random = new Random(); for (int i = 0; i < TRIES; i++) { double x = random.nextDouble(); double y = random.nextDouble(); double h = Math.hypot(x, y); BigDecimal decimalX = BigDecimal.valueOf(x); BigDecimal decimalY = BigDecimal.valueOf(y); assertEquals(BigMath.hypot(x, y, CONTEXT).doubleValue() / h, 1, DBL_TOLERANCE); assertEquals(BigMath.hypot(decimalX, decimalY, CONTEXT).doubleValue() / h, 1, DBL_TOLERANCE); } }
@Test(timeOut = TRIES * CBRT_FACTOR) public void testCubeRoot() throws Exception { Random random = new Random(); for (int i = 0; i < TRIES; i++) { // BigInteger version BigInteger integer = getRandomBigInteger(random); MathContext context = getContextFor(integer, 3); BigDecimal nthRoot = BigMath.cbrt(integer, context); assertEquals(nthRoot.pow(3, context).toBigInteger(), integer); // BigDecimal version BigDecimal decimal = getRandomBigDecimal(random, CONTEXT); BigDecimal sqrRoot = BigMath.cbrt(decimal, CONTEXT); assertEquals( sqrRoot.pow(3).round(CONTEXT).stripTrailingZeros(), decimal.stripTrailingZeros()); } }
@Test public void testNthRoot() throws Exception { Random random = new Random(); // because it is very time-consuming. for (int i = 0; i < Math.sqrt(TRIES); i++) { int n = 2 + random.nextInt(ROOT_BOUND - 2); // BigInteger version BigInteger integer = getRandomBigInteger(random); MathContext context = getContextFor(integer, n); BigDecimal nthRoot = BigMath.nthRoot(integer, n, context); assertEquals(nthRoot.pow(n, context).toBigInteger(), integer); // BigDecimal version BigDecimal decimal = getRandomBigDecimal(random, CONTEXT); nthRoot = BigMath.nthRoot(decimal, n, CONTEXT); assertEquals( nthRoot.pow(n, CONTEXT).round(CONTEXT).stripTrailingZeros(), decimal.stripTrailingZeros()); } }