Search in sources :

Example 66 with MathContext

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

the class BigDecimalMath method logUsingTwoThree.

private static BigDecimal logUsingTwoThree(BigDecimal x, MathContext mathContext) {
    MathContext mc = new MathContext(mathContext.getPrecision() * 19 / 10 + 10, mathContext.getRoundingMode());
    // System.out.println("logUsingTwoThree(" + x + " " + mathContext + ") precision " + mc);
    int factorOfTwo = 0;
    int powerOfTwo = 1;
    int factorOfThree = 0;
    int powerOfThree = 1;
    double value = x.doubleValue();
    if (value < 0.01) {
    // do nothing
    } else if (value < 0.1) {
        // never happens when called by logUsingExponent()
        while (value < 0.6) {
            value *= 2;
            factorOfTwo--;
            powerOfTwo *= 2;
        }
    } else if (value < 0.115) {
        // (0.1 - 0.11111 - 0.115) -> (0.9 - 1.0 - 1.035)
        factorOfThree = -2;
        powerOfThree = 9;
    } else if (value < 0.14) {
        // (0.115 - 0.125 - 0.14) -> (0.92 - 1.0 - 1.12)
        factorOfTwo = -3;
        powerOfTwo = 8;
    } else if (value < 0.2) {
        // (0.14 - 0.16667 - 0.2) - (0.84 - 1.0 - 1.2)
        factorOfTwo = -1;
        powerOfTwo = 2;
        factorOfThree = -1;
        powerOfThree = 3;
    } else if (value < 0.3) {
        // (0.2 - 0.25 - 0.3) -> (0.8 - 1.0 - 1.2)
        factorOfTwo = -2;
        powerOfTwo = 4;
    } else if (value < 0.42) {
        // (0.3 - 0.33333 - 0.42) -> (0.9 - 1.0 - 1.26)
        factorOfThree = -1;
        powerOfThree = 3;
    } else if (value < 0.7) {
        // (0.42 - 0.5 - 0.7) -> (0.84 - 1.0 - 1.4)
        factorOfTwo = -1;
        powerOfTwo = 2;
    } else if (value < 1.4) {
    // (0.7 - 1.0 - 1.4) -> (0.7 - 1.0 - 1.4)
    // do nothing
    } else if (value < 2.5) {
        // (1.4 - 2.0 - 2.5) -> (0.7 - 1.0 - 1.25)
        factorOfTwo = 1;
        powerOfTwo = 2;
    } else if (value < 3.5) {
        // (2.5 - 3.0 - 3.5) -> (0.833333 - 1.0 - 1.166667)
        factorOfThree = 1;
        powerOfThree = 3;
    } else if (value < 5.0) {
        // (3.5 - 4.0 - 5.0) -> (0.875 - 1.0 - 1.25)
        factorOfTwo = 2;
        powerOfTwo = 4;
    } else if (value < 7.0) {
        // (5.0 - 6.0 - 7.0) -> (0.833333 - 1.0 - 1.166667)
        factorOfThree = 1;
        powerOfThree = 3;
        factorOfTwo = 1;
        powerOfTwo = 2;
    } else if (value < 8.5) {
        // (7.0 - 8.0 - 8.5) -> (0.875 - 1.0 - 1.0625)
        factorOfTwo = 3;
        powerOfTwo = 8;
    } else if (value < 10.0) {
        // (8.5 - 9.0 - 10.0) -> (0.94444 - 1.0 - 1.11111)
        factorOfThree = 2;
        powerOfThree = 9;
    } else {
        while (value > 1.4) {
            // never happens when called by logUsingExponent()
            value /= 2;
            factorOfTwo++;
            powerOfTwo *= 2;
        }
    }
    BigDecimal correctedX = x;
    BigDecimal result = ZERO;
    if (factorOfTwo > 0) {
        correctedX = correctedX.divide(valueOf(powerOfTwo), mc);
        result = result.add(logTwo(mc).multiply(valueOf(factorOfTwo), mc), mc);
    } else if (factorOfTwo < 0) {
        correctedX = correctedX.multiply(valueOf(powerOfTwo), mc);
        result = result.subtract(logTwo(mc).multiply(valueOf(-factorOfTwo), mc), mc);
    }
    if (factorOfThree > 0) {
        correctedX = correctedX.divide(valueOf(powerOfThree), mc);
        result = result.add(logThree(mc).multiply(valueOf(factorOfThree), mc), mc);
    } else if (factorOfThree < 0) {
        correctedX = correctedX.multiply(valueOf(powerOfThree), mc);
        result = result.subtract(logThree(mc).multiply(valueOf(-factorOfThree), mc), mc);
    }
    if (x == correctedX && result == ZERO) {
        return logUsingNewton(x, mathContext);
    }
    result = result.add(logUsingNewton(correctedX, mc), mc);
    return result;
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 67 with MathContext

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

the class BigDecimalMath method acot.

/**
 * Calculates the inverse cotangens (arc cotangens) of {@link BigDecimal} x.
 *
 * <p>See: <a href="http://en.wikipedia.org/wiki/Arccotangens">Wikipedia: Arccotangens</a></p>
 *
 * @param x the {@link BigDecimal} to calculate the arc cotangens for
 * @param mathContext the {@link MathContext} used for the result
 * @return the calculated arc cotangens {@link BigDecimal} with the precision specified in the <code>mathContext</code>
 */
public static BigDecimal acot(BigDecimal x, MathContext mathContext) {
    MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode());
    BigDecimal result = pi(mc).divide(TWO, mc).subtract(atan(x, mc), mc);
    return result.round(mathContext);
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 68 with MathContext

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

the class BigDecimalMath method cot.

/**
 * Calculates the cotangens of {@link BigDecimal} x.
 *
 * <p>See: <a href="http://en.wikipedia.org/wiki/Cotangens">Wikipedia: Cotangens</a></p>
 *
 * @param x the {@link BigDecimal} to calculate the cotangens for
 * @param mathContext the {@link MathContext} used for the result
 * @return the calculated cotanges {@link BigDecimal} with the precision specified in the <code>mathContext</code>
 * @throws ArithmeticException if x = 0
 */
public static BigDecimal cot(BigDecimal x, MathContext mathContext) {
    if (x.signum() == 0) {
        throw new ArithmeticException("Illegal cot(x) for x = 0");
    }
    MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode());
    BigDecimal result = cos(x, mc).divide(sin(x, mc), mc).round(mathContext);
    return result.round(mathContext);
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 69 with MathContext

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

the class BigDecimalMath method atanh.

/**
 * Calculates the arc hyperbolic tangens (inverse hyperbolic tangens ) 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 tanges for
 * @param mathContext the {@link MathContext} used for the result
 * @return the calculated arc hyperbolic tangens {@link BigDecimal} with the precision specified in the <code>mathContext</code>
 */
public static BigDecimal atanh(BigDecimal x, MathContext mathContext) {
    MathContext mc = new MathContext(mathContext.getPrecision() + 6, mathContext.getRoundingMode());
    BigDecimal result = log(ONE.add(x, mc).divide(ONE.subtract(x, mc), mc), mc).divide(TWO, mc);
    return result.round(mathContext);
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 70 with MathContext

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

the class BigDecimalMath method pow.

/**
 * Calculates {@link BigDecimal} x to the power of {@link BigDecimal} y (x<sup>y</sup>).
 *
 * @param x the {@link BigDecimal} value to take to the power
 * @param y the {@link BigDecimal} value to serve as exponent
 * @param mathContext the {@link MathContext} used for the result
 * @return the calculated x to the power of y with the precision specified in the <code>mathContext</code>
 */
public static BigDecimal pow(BigDecimal x, BigDecimal y, MathContext mathContext) {
    if (x.signum() == 0) {
        switch(y.signum()) {
            case 0:
                return ONE;
            case 1:
                return ZERO;
        }
    }
    try {
        long longValue = y.longValueExact();
        return pow(x, longValue, mathContext);
    } catch (ArithmeticException ex) {
    // ignored
    }
    if (fractionalPart(y).signum() == 0) {
        return powInteger(x, y, mathContext);
    }
    // x^y = exp(y*log(x))
    MathContext mc = new MathContext(mathContext.getPrecision() + 6, mathContext.getRoundingMode());
    BigDecimal result = exp(y.multiply(log(x, mc), mc), mc);
    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