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