use of java.math.MathContext in project big-math by eobermuhlner.
the class BigDecimalMath method root.
/**
* Calculates the n'th root of {@link BigDecimal} x.
*
* <p>See <a href="http://en.wikipedia.org/wiki/Square_root">Wikipedia: Square root</a></p>
* @param x the {@link BigDecimal} value to calculate the n'th root
* @param n the {@link BigDecimal} defining the root
* @param mathContext the {@link MathContext} used for the result
*
* @return the calculated n'th root of x with the precision specified in the <code>mathContext</code>
* @throws ArithmeticException if x < 0
*/
public static BigDecimal root(BigDecimal x, BigDecimal n, MathContext mathContext) {
switch(x.signum()) {
case 0:
return ZERO;
case -1:
throw new ArithmeticException("Illegal root(x) for x < 0: x = " + x);
}
if (n.compareTo(BigDecimal.ONE) <= 0) {
MathContext mc = new MathContext(mathContext.getPrecision() + 6, mathContext.getRoundingMode());
return pow(x, BigDecimal.ONE.divide(n, mc), mathContext);
}
int maxPrecision = mathContext.getPrecision() + 4;
BigDecimal acceptableError = ONE.movePointLeft(mathContext.getPrecision() + 1);
BigDecimal nMinus1 = n.subtract(ONE);
BigDecimal result = x.divide(TWO, MathContext.DECIMAL32);
// first approximation has really bad precision
int adaptivePrecision = 2;
BigDecimal step;
do {
adaptivePrecision = adaptivePrecision * 3;
if (adaptivePrecision > maxPrecision) {
adaptivePrecision = maxPrecision;
}
MathContext mc = new MathContext(adaptivePrecision, mathContext.getRoundingMode());
step = x.divide(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 BigDecimalMath method sinh.
/**
* Calculates the hyperbolic sine of {@link BigDecimal} x.
*
* <p>See: <a href="https://en.wikipedia.org/wiki/Hyperbolic_function">Wikipedia: Hyperbolic function</a></p>
*
* @param x the {@link BigDecimal} to calculate the hyperbolic sine for
* @param mathContext the {@link MathContext} used for the result
* @return the calculated hyperbolic sine {@link BigDecimal} with the precision specified in the <code>mathContext</code>
*/
public static BigDecimal sinh(BigDecimal x, MathContext mathContext) {
MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode());
BigDecimal result = SinhCalculator.INSTANCE.calculate(x, mc);
return result.round(mathContext);
}
use of java.math.MathContext in project big-math by eobermuhlner.
the class BigDecimalMath method atan.
/**
* Calculates the arc tangens (inverted tangens) of {@link BigDecimal} x.
*
* <p>See: <a href="http://en.wikipedia.org/wiki/Arctangens">Wikipedia: Arctangens</a></p>
*
* @param x the {@link BigDecimal} to calculate the arc tangens for
* @param mathContext the {@link MathContext} used for the result
* @return the calculated arc tangens {@link BigDecimal} with the precision specified in the <code>mathContext</code>
*/
public static BigDecimal atan(BigDecimal x, MathContext mathContext) {
MathContext mc = new MathContext(mathContext.getPrecision() + 6, mathContext.getRoundingMode());
x = x.divide(sqrt(ONE.add(x.multiply(x, mc), mc), mc), mc);
BigDecimal result = asin(x, mc);
return result.round(mathContext);
}
use of java.math.MathContext in project big-math by eobermuhlner.
the class BigDecimalMath method acoth.
/**
* Calculates the arc hyperbolic cotangens (inverse hyperbolic cotangens) of {@link BigDecimal} x.
*
* <p>See: <a href="https://en.wikipedia.org/wiki/Hyperbolic_function">Wikipedia: Hyperbolic function</a></p>
*
* @param x the {@link BigDecimal} to calculate the arc hyperbolic cotangens for
* @param mathContext the {@link MathContext} used for the result
* @return the calculated arc hyperbolic cotangens {@link BigDecimal} with the precision specified in the <code>mathContext</code>
*/
public static BigDecimal acoth(BigDecimal x, MathContext mathContext) {
MathContext mc = new MathContext(mathContext.getPrecision() + 6, mathContext.getRoundingMode());
BigDecimal result = log(x.add(ONE, mc).divide(x.subtract(ONE, mc), mc), mc).divide(TWO, mc);
return result.round(mathContext);
}
use of java.math.MathContext in project big-math by eobermuhlner.
the class BigComplexMathTest method assertPrecisionCalculation.
private void assertPrecisionCalculation(BigComplex expected, Function<MathContext, BigComplex> precisionCalculation, int startPrecision, int endPrecision) {
int precision = startPrecision;
while (precision <= endPrecision) {
MathContext mathContext = new MathContext(precision);
System.out.println("precision=" + precision + " " + expected.round(mathContext));
assertEquals("precision=" + precision, expected.round(mathContext).toString(), precisionCalculation.apply(mathContext).toString());
precision += 5;
}
}
Aggregations