Search in sources :

Example 91 with MathContext

use of java.math.MathContext in project big-math by eobermuhlner.

the class FunctionTable method printTableSin.

public static void printTableSin() {
    MathContext mathContext = new MathContext(20);
    printTable(0, 10, 0.1, Arrays.asList("BigDecimalMath.sin", "Math.sin"), Arrays.asList(x -> BigDecimalMath.sin(x, mathContext), x -> BigDecimal.valueOf(Math.sin(x.doubleValue()))));
}
Also used : BigDecimal(java.math.BigDecimal) Arrays(java.util.Arrays) List(java.util.List) MathContext(java.math.MathContext) BigDecimalMath(ch.obermuhlner.math.big.BigDecimalMath) Function(java.util.function.Function) MathContext(java.math.MathContext)

Example 92 with MathContext

use of java.math.MathContext in project big-math by eobermuhlner.

the class BigDecimalMathExperimental method sqrtUsingHalleyPrint.

public static BigDecimal sqrtUsingHalleyPrint(BigDecimal x, MathContext mathContext) {
    switch(x.signum()) {
        case 0:
            return ZERO;
        case -1:
            throw new ArithmeticException("Illegal sqrt(x) for x < 0: x = " + x);
    }
    MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode());
    BigDecimal acceptableError = ONE.movePointLeft(mathContext.getPrecision() + 1);
    BigDecimal threeX = x.multiply(THREE);
    BigDecimal result = BigDecimal.valueOf(Math.sqrt(x.doubleValue()));
    BigDecimal last;
    do {
        last = result;
        BigDecimal resultSquare = result.multiply(result);
        BigDecimal divisor = resultSquare.multiply(THREE).add(x);
        result = resultSquare.add(threeX).multiply(result).divide(divisor, mc);
        System.out.printf("%5d, ", countSameCharacters(last.toPlainString(), result.toPlainString()));
    } while (result.subtract(last).abs().compareTo(acceptableError) > 0);
    return result.round(mathContext);
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 93 with MathContext

use of java.math.MathContext in project big-math by eobermuhlner.

the class BigDecimalMathExperimental method rootAdaptivePrecision.

public static BigDecimal rootAdaptivePrecision(BigDecimal n, BigDecimal x, MathContext mathContext, int initialPrecision) {
    switch(x.signum()) {
        case 0:
            return ZERO;
        case -1:
            throw new ArithmeticException("Illegal root(x) for x < 0: x = " + x);
    }
    int maxPrecision = mathContext.getPrecision() + 4;
    BigDecimal acceptableError = ONE.movePointLeft(mathContext.getPrecision() + 1);
    BigDecimal nMinus1 = n.subtract(ONE);
    BigDecimal result = x.divide(TWO, MathContext.DECIMAL32);
    int adaptivePrecision = initialPrecision;
    BigDecimal step;
    do {
        adaptivePrecision = adaptivePrecision * 3;
        if (adaptivePrecision > maxPrecision) {
            adaptivePrecision = maxPrecision;
        }
        MathContext mc = new MathContext(adaptivePrecision, mathContext.getRoundingMode());
        step = x.divide(BigDecimalMath.pow(result, nMinus1, mc), mc).subtract(result, mc).divide(n, mc);
        result = result.add(step, mc);
    } while (adaptivePrecision < maxPrecision || step.abs().compareTo(acceptableError) > 0);
    return result.round(mathContext);
}
Also used : BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext)

Example 94 with MathContext

use of java.math.MathContext in project big-math by eobermuhlner.

the class BigDecimalMathExperimental method expReducing.

public static BigDecimal expReducing(BigDecimal x, MathContext mathContext, int reduce) {
    MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode());
    x = x.divide(valueOf(reduce), mc);
    BigDecimal result = ExpCalculator.INSTANCE.calculate(x, mc);
    result = BigDecimalMath.pow(result, reduce, mc);
    return result.round(mathContext);
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 95 with MathContext

use of java.math.MathContext in project big-math by eobermuhlner.

the class BigDecimalMathExperimental method acosUsingNewton.

public static BigDecimal acosUsingNewton(BigDecimal x, MathContext mathContext) {
    if (x.compareTo(ONE) > 0) {
        throw new ArithmeticException("Illegal acos(x) for x > 1: x = " + x);
    }
    if (x.compareTo(MINUS_ONE) < 0) {
        throw new ArithmeticException("Illegal acos(x) for x < -1: x = " + x);
    }
    MathContext mc = new MathContext(mathContext.getPrecision() + 6, mathContext.getRoundingMode());
    BigDecimal result = BigDecimalMath.pi(mc).divide(TWO, mc).subtract(asinUsingNewton(x, mc), mc);
    return result.round(mathContext);
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

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