Search in sources :

Example 76 with MathContext

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 &lt; 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);
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 77 with 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);
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 78 with 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);
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 79 with 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);
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 80 with 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;
    }
}
Also used : MathContext(java.math.MathContext)

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