Search in sources :

Example 81 with MathContext

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);
}
Also used : BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext)

Example 82 with MathContext

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);
    }
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 83 with 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));
}
Also used : BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext) Test(org.junit.Test)

Example 84 with MathContext

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);
    }
}
Also used : Random(java.util.Random) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 85 with MathContext

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());
        }
    }
}
Also used : Random(java.util.Random) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext)

Aggregations

MathContext (java.math.MathContext)237 BigDecimal (java.math.BigDecimal)224 RoundingMode (java.math.RoundingMode)73 BigInteger (java.math.BigInteger)71 List (java.util.List)9 Test (org.junit.Test)8 BigDecimalMath (ch.obermuhlner.math.big.BigDecimalMath)7 Arrays (java.util.Arrays)7 Function (java.util.function.Function)7 StopWatch (ch.obermuhlner.math.big.example.StopWatch)3 IOException (java.io.IOException)3 Random (java.util.Random)3 Context (ch.obermuhlner.math.big.BigFloat.Context)2 FileWriter (java.io.FileWriter)2 PrintWriter (java.io.PrintWriter)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 RandomDataGenerator (org.apache.commons.math3.random.RandomDataGenerator)2 Well19937c (org.apache.commons.math3.random.Well19937c)2 DataCell (org.knime.core.data.DataCell)2