Search in sources :

Example 96 with MathContext

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

the class BigDecimalMathExperimental method logUsingSqrt.

public static BigDecimal logUsingSqrt(BigDecimal x, MathContext mathContext, BiFunction<BigDecimal, MathContext, BigDecimal> logFunction) {
    MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode());
    BigDecimal sqrtX = BigDecimalMath.sqrt(x, mc);
    BigDecimal result = logFunction.apply(sqrtX, mc).multiply(TWO, mc);
    return result.round(mathContext);
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 97 with MathContext

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

the class BigDecimalMathExperimental method exp.

public static BigDecimal exp(BigDecimal x, MathContext mathContext) {
    MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode());
    BigDecimal result = ExpCalculator.INSTANCE.calculate(x, mc);
    return result.round(mathContext);
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 98 with MathContext

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

the class BigDecimalMathExperimental method logUsingNewtonFixPrecision.

public static BigDecimal logUsingNewtonFixPrecision(BigDecimal x, MathContext mathContext) {
    // https://en.wikipedia.org/wiki/Natural_logarithm in chapter 'High Precision'
    // y = y + 2 * (x-exp(y)) / (x+exp(y))
    MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode());
    BigDecimal acceptableError = ONE.movePointLeft(mathContext.getPrecision() + 1);
    BigDecimal result = BigDecimal.valueOf(Math.log(x.doubleValue()));
    BigDecimal step;
    do {
        BigDecimal expY = BigDecimalMath.exp(result, mc);
        step = TWO.multiply(x.subtract(expY, mc), mc).divide(x.add(expY, mc), mc);
        result = result.add(step);
    } while (step.abs().compareTo(acceptableError) > 0);
    return result.round(mathContext);
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 99 with MathContext

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

the class BigDecimalMathExperimental method sqrtUsingHalley.

public static BigDecimal sqrtUsingHalley(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);
    } while (result.subtract(last).abs().compareTo(acceptableError) > 0);
    return result.round(mathContext);
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 100 with MathContext

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

the class BigDecimalMathExperimental method sqrtUsingNewtonPrint.

// variations on sqrt()
public static BigDecimal sqrtUsingNewtonPrint(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 result = BigDecimal.valueOf(Math.sqrt(x.doubleValue()));
    BigDecimal last;
    do {
        last = result;
        result = x.divide(result, mc).add(last).divide(TWO, 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)

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