use of java.math.MathContext in project big-math by eobermuhlner.
the class BigDecimalMathTest method assertPrecisionCalculation.
private void assertPrecisionCalculation(Function<MathContext, BigDecimal> precisionCalculation, int startPrecision, int endPrecision) {
BigDecimal expected = precisionCalculation.apply(new MathContext(endPrecision * 2));
// System.out.println("reference expected: " + expected);
assertPrecisionCalculation(expected, precisionCalculation, startPrecision, endPrecision);
}
use of java.math.MathContext in project big-math by eobermuhlner.
the class BigDecimalMathTest method testLog10WithPositivePowersOfTen.
@Test
public void testLog10WithPositivePowersOfTen() {
MathContext mathContext = new MathContext(50);
BigDecimal x = new BigDecimal("1");
BigDecimal expectedLog10 = new BigDecimal(0);
for (int i = 0; i < 20; i++) {
BigDecimal actualLog10 = BigDecimalMath.log10(x, mathContext);
assertEquals(true, expectedLog10.compareTo(actualLog10) == 0);
x = x.multiply(BigDecimal.TEN, mathContext);
expectedLog10 = expectedLog10.add(BigDecimal.ONE, mathContext);
}
}
use of java.math.MathContext in project big-math by eobermuhlner.
the class BigDecimalMathTest method testAtan2.
@Test
public void testAtan2() {
BigDecimal piHalf = BigDecimalMath.pi(new MathContext(MC.getPrecision() + 10)).divide(BigDecimal.valueOf(2), MC);
assertEquals(piHalf, BigDecimalMath.atan2(BigDecimal.TEN, BigDecimal.ZERO, MC));
assertEquals(piHalf.negate(), BigDecimalMath.atan2(BigDecimal.TEN.negate(), BigDecimal.ZERO, MC));
}
use of java.math.MathContext in project big-math by eobermuhlner.
the class BigDecimalMathTest method assertRandomCalculation.
private void assertRandomCalculation(int count, String function1Name, String function2Name, BiFunction<Random, MathContext, BigDecimal> xFunction, BiFunction<BigDecimal, MathContext, BigDecimal> calculation1, BiFunction<BigDecimal, MathContext, BigDecimal> calculation2) {
Random random = new Random(1);
for (int i = 0; i < count; i++) {
int numberPrecision = random.nextInt(100) + 1;
int calculationPrecision = numberPrecision + 10;
MathContext numberMathContext = new MathContext(numberPrecision);
BigDecimal x = xFunction.apply(random, numberMathContext);
MathContext calculationMathContext = new MathContext(calculationPrecision);
BigDecimal y1 = calculation1.apply(x, calculationMathContext);
BigDecimal y2 = calculation2.apply(x, calculationMathContext);
BigDecimal error = y2.subtract(y1, calculationMathContext).abs();
BigDecimal acceptableError = BigDecimalMath.pow(BigDecimal.TEN, -numberPrecision, calculationMathContext);
String description = "x=(" + x + ") " + function1Name + "=" + y1 + " " + function2Name + "=" + y2;
assertEquals(description + " precision=" + numberPrecision, true, error.compareTo(acceptableError) <= 0);
}
}
use of java.math.MathContext in project big-math by eobermuhlner.
the class BigDecimalMathTest method assertRandomCalculation.
private void assertRandomCalculation(int count, String functionName, Function<Random, Double> xFunction, Function<Double, Double> doubleFunction, BiFunction<BigDecimal, MathContext, BigDecimal> calculation) {
Random random = new Random(1);
for (int i = 0; i < count; i++) {
int precision = random.nextInt(RANDOM_MAX_PRECISION) + 1;
Double xDouble = xFunction.apply(random);
BigDecimal x = BigDecimal.valueOf(xDouble);
String description = functionName + "(" + x + ")";
System.out.println("Testing " + description + " precision=" + precision);
MathContext mathContext = new MathContext(precision);
BigDecimal result = calculation.apply(x, mathContext);
if (doubleFunction != null && precision > MC_CHECK_DOUBLE.getPrecision() + 2) {
assertEquals(description + " vs. double function : " + result, toCheck(doubleFunction.apply(xDouble)), toCheck(result));
}
MathContext referenceMathContext = new MathContext(precision * 2 + 20);
BigDecimal referenceResult = calculation.apply(x, referenceMathContext);
BigDecimal expected = referenceResult.round(mathContext);
if (expected.compareTo(result) != 0) {
assertEquals(description + " referencePrecision=" + referenceMathContext.getPrecision() + " referenceResult=" + referenceResult, expected.toString(), result.toString());
}
}
}
Aggregations