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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations