Search in sources :

Example 61 with MathContext

use of java.math.MathContext in project robovm by robovm.

the class BigDecimalTest method testRound.

public void testRound() {
    BigDecimal bigDecimal = new BigDecimal("0.999999999999999");
    BigDecimal rounded = bigDecimal.round(new MathContext(2, RoundingMode.FLOOR));
    assertEquals("0.99", rounded.toString());
}
Also used : BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext)

Example 62 with MathContext

use of java.math.MathContext in project robovm by robovm.

the class OldBigDecimalArithmeticTest method testAddMathContextNonTrivial.

public void testAddMathContextNonTrivial() {
    MathContext mc;
    BigDecimal a, b, res;
    mc = new MathContext(17, RoundingMode.FLOOR);
    a = new BigDecimal("123456789012345.678");
    b = new BigDecimal("100000000000000.009");
    assertEquals("incorrect value", "123456789012345.67", a.round(mc).toString());
    assertEquals("incorrect value", "100000000000000.00", b.round(mc).toString());
    assertEquals("incorrect value", "223456789012345.67", a.round(mc).add(b.round(mc)).toString());
    res = a.add(b, mc);
    assertEquals("incorrect value", "223456789012345.68", res.toString());
    mc = new MathContext(33, RoundingMode.UNNECESSARY);
    a = new BigDecimal("1234567890123456789012345678.9012395");
    b = new BigDecimal("1000000000000000090000000000.0000005");
    res = a.add(b, mc);
    assertEquals("Incorrect value!", "2234567890123456879012345678.90124", res.toString());
    assertEquals("Incorrect scale!", 5, res.scale());
    assertEquals("Incorrect precision!", 33, res.precision());
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 63 with MathContext

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

the class BigComplexMath method exp.

/**
 * Calculates the natural exponent of {@link BigComplex} x (e<sup>x</sup>) in the complex domain.
 *
 * <p>See: <a href="https://en.wikipedia.org/wiki/Exponential_function#Complex_plane">Wikipedia: Exponent (Complex plane)</a></p>
 *
 * @param x the {@link BigComplex} to calculate the exponent for
 * @param mathContext the {@link MathContext} used for the result
 * @return the calculated exponent {@link BigComplex} with the precision specified in the <code>mathContext</code>
 */
public static BigComplex exp(BigComplex x, MathContext mathContext) {
    MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode());
    BigDecimal expRe = BigDecimalMath.exp(x.re, mc);
    return BigComplex.valueOf(expRe.multiply(BigDecimalMath.cos(x.im, mc), mc).round(mathContext), expRe.multiply(BigDecimalMath.sin(x.im, mc), mc)).round(mathContext);
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 64 with MathContext

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

the class BigComplexMath method toRangePi.

// TODO add root() for the k'th root - https://math.stackexchange.com/questions/322481/principal-nth-root-of-a-complex-number
private static BigDecimal toRangePi(BigDecimal angle, MathContext mathContext) {
    MathContext mc = new MathContext(mathContext.getPrecision() + 4, mathContext.getRoundingMode());
    BigDecimal pi = BigDecimalMath.pi(mc);
    if (angle.compareTo(pi.negate()) < 0) {
        return angle.add(pi, mc).add(pi, mc).round(mathContext);
    }
    if (angle.compareTo(pi) > 0) {
        return angle.subtract(pi, mc).subtract(pi, mc).round(mathContext);
    }
    return angle;
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 65 with MathContext

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

the class BigDecimalMath method piChudnovski.

private static BigDecimal piChudnovski(MathContext mathContext) {
    MathContext mc = new MathContext(mathContext.getPrecision() + 10, mathContext.getRoundingMode());
    final BigDecimal value24 = BigDecimal.valueOf(24);
    final BigDecimal value640320 = BigDecimal.valueOf(640320);
    final BigDecimal value13591409 = BigDecimal.valueOf(13591409);
    final BigDecimal value545140134 = BigDecimal.valueOf(545140134);
    final BigDecimal valueDivisor = value640320.pow(3).divide(value24, mc);
    BigDecimal sumA = BigDecimal.ONE;
    BigDecimal sumB = BigDecimal.ZERO;
    BigDecimal a = BigDecimal.ONE;
    // -(6*k - 5)
    long dividendTerm1 = 5;
    // 2*k - 1
    long dividendTerm2 = -1;
    // 6*k - 1
    long dividendTerm3 = -1;
    BigDecimal kPower3 = BigDecimal.ZERO;
    long iterationCount = (mc.getPrecision() + 13) / 14;
    for (long k = 1; k <= iterationCount; k++) {
        BigDecimal valueK = BigDecimal.valueOf(k);
        dividendTerm1 += -6;
        dividendTerm2 += 2;
        dividendTerm3 += 6;
        BigDecimal dividend = BigDecimal.valueOf(dividendTerm1).multiply(BigDecimal.valueOf(dividendTerm2)).multiply(BigDecimal.valueOf(dividendTerm3));
        kPower3 = valueK.pow(3);
        BigDecimal divisor = kPower3.multiply(valueDivisor, mc);
        a = a.multiply(dividend).divide(divisor, mc);
        BigDecimal b = valueK.multiply(a, mc);
        sumA = sumA.add(a);
        sumB = sumB.add(b);
    }
    final BigDecimal value426880 = BigDecimal.valueOf(426880);
    final BigDecimal value10005 = BigDecimal.valueOf(10005);
    final BigDecimal factor = value426880.multiply(sqrt(value10005, mc));
    BigDecimal pi = factor.divide(value13591409.multiply(sumA, mc).add(value545140134.multiply(sumB, mc)), mc);
    return pi.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