Search in sources :

Example 36 with MathContext

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

the class OldBigDecimalConstructorsTest method testConstrCharIntIntMathContextException1.

/**
     * new BigDecimal(char[] value, int offset, int len, MathContext mc);
     */
public void testConstrCharIntIntMathContextException1() {
    char[] value = { '-', '1', '2', '3', '8', '0', '.', '4', '7', '3', '8', 'E', '-', '4', '2', '3' };
    int offset = 3;
    int len = 120;
    int precision = 4;
    RoundingMode rm = RoundingMode.CEILING;
    MathContext mc = new MathContext(precision, rm);
    try {
        new BigDecimal(value, offset, len, mc);
        fail("NumberFormatException has not been thrown");
    } catch (NumberFormatException e) {
    }
}
Also used : RoundingMode(java.math.RoundingMode) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 37 with MathContext

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

the class OldBigDecimalConstructorsTest method testConstrStringMathContext.

/**
     * new BigDecimal(String value, MathContext)
     */
public void testConstrStringMathContext() {
    String a = "-238768787678287e214";
    int precision = 5;
    RoundingMode rm = RoundingMode.CEILING;
    MathContext mc = new MathContext(precision, rm);
    String res = "-23876";
    int resScale = -224;
    BigDecimal result = new BigDecimal(a, mc);
    assertEquals("incorrect value", res, result.unscaledValue().toString());
    assertEquals("incorrect scale", resScale, result.scale());
    // Now test more than just RoundingMode.CEILING:
    //
    String biStr = new String("12345678901234567890123456789012345.0E+10");
    String nbiStr = new String("-12345678901234567890123456789012345.E+10");
    BigDecimal bd;
    mc = new MathContext(31, RoundingMode.UP);
    bd = new BigDecimal(biStr, mc);
    assertEquals("incorrect value", "1.234567890123456789012345678902E+44", bd.toString());
    bd = new BigDecimal(nbiStr, mc);
    assertEquals("incorrect value", "-1.234567890123456789012345678902E+44", bd.toString());
    mc = new MathContext(28, RoundingMode.DOWN);
    bd = new BigDecimal(biStr, mc);
    assertEquals("incorrect value", "1.234567890123456789012345678E+44", bd.toString());
    bd = new BigDecimal(nbiStr, mc);
    assertEquals("incorrect value", "-1.234567890123456789012345678E+44", bd.toString());
    mc = new MathContext(33, RoundingMode.CEILING);
    bd = new BigDecimal(biStr, mc);
    assertEquals("incorrect value", "1.23456789012345678901234567890124E+44", bd.toString());
    bd = new BigDecimal(nbiStr, mc);
    assertEquals("incorrect value", "-1.23456789012345678901234567890123E+44", bd.toString());
    mc = new MathContext(34, RoundingMode.UNNECESSARY);
    try {
        bd = new BigDecimal(biStr, mc);
        fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    } catch (ArithmeticException e) {
    // expected
    }
    try {
        bd = new BigDecimal(nbiStr, mc);
        fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    } catch (ArithmeticException e) {
    // expected
    }
    mc = new MathContext(7, RoundingMode.FLOOR);
    bd = new BigDecimal("1000000.9", mc);
    assertEquals("incorrect value", "1000000", bd.toString());
}
Also used : RoundingMode(java.math.RoundingMode) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 38 with MathContext

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

the class OldBigDecimalArithmeticTest method testSubtractMathContextNonTrivial.

public void testSubtractMathContextNonTrivial() {
    MathContext mc;
    BigDecimal a, b, res;
    mc = new MathContext(17, RoundingMode.FLOOR);
    a = new BigDecimal("12345678901234567.8");
    b = new BigDecimal("10000000000000000.9");
    assertEquals("incorrect value", "2345678901234567", a.round(mc).subtract(b.round(mc)).toString());
    res = a.subtract(b, mc);
    assertEquals("incorrect value", "2345678901234566.9", res.toString());
    assertEquals("Incorrect scale!", 1, res.scale());
    assertEquals("Incorrect precision!", 17, res.precision());
    mc = new MathContext(33, RoundingMode.UNNECESSARY);
    a = new BigDecimal("1234567890123456789012345678.9012395");
    b = new BigDecimal("1000000000000000090000000000.0000005");
    res = a.subtract(b, mc);
    assertEquals("incorrect value", "234567890123456699012345678.901239", res.toString());
    assertEquals("Incorrect scale!", 6, res.scale());
    assertEquals("Incorrect precision!", 33, res.precision());
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 39 with MathContext

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

the class OldBigDecimalArithmeticTest method testMultiplyMathContextNonTrivial.

public void testMultiplyMathContextNonTrivial() {
    MathContext mc;
    BigDecimal a, b, res;
    mc = new MathContext(17, RoundingMode.FLOOR);
    a = new BigDecimal("92345678901234567.8");
    b = new BigDecimal("10000000000000000.9");
    res = a.round(mc).multiply(b.round(mc));
    assertEquals("incorrect value", "923456789012345670000000000000000", res.toString());
    res = res.round(mc);
    assertEquals("incorrect value", "9.2345678901234567E+32", res.toString());
    res = a.multiply(b, mc);
    assertEquals("incorrect value", "9.2345678901234576E+32", res.toString());
    assertEquals("Incorrect scale!", -16, res.scale());
    assertEquals("Incorrect precision!", 17, res.precision());
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 40 with MathContext

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

the class OldBigDecimalArithmeticTest method testPowMathContextNonTrivial.

public void testPowMathContextNonTrivial() {
    MathContext mc;
    BigDecimal a, b, res;
    mc = new MathContext(7, RoundingMode.FLOOR);
    a = new BigDecimal("1000000.9");
    assertEquals("incorrect value", "1.000000E+6000", a.round(mc).pow(1000).round(mc).toString());
    res = a.pow(1000, mc);
    assertEquals("incorrect value", "1.000900E+6000", res.toString());
    mc = new MathContext(4, RoundingMode.FLOOR);
    a = new BigDecimal("1000.9");
    assertEquals("incorrect value", "1.000E+3000", a.round(mc).pow(1000).round(mc).toString());
    res = a.pow(1000, mc);
    assertEquals("incorrect value", "2.458E+3000", res.toString());
    mc = new MathContext(2, RoundingMode.UNNECESSARY);
    a = new BigDecimal("1234");
    try {
        res = a.pow(-2, mc);
        fail("ArithmeticException is not thrown");
    } catch (ArithmeticException e) {
    // expected
    }
    a = new BigDecimal("100");
    mc = new MathContext(4, RoundingMode.UNNECESSARY);
    res = a.pow(-2, mc);
    assertEquals("incorrect value", "0.0001", res.toString());
    a = new BigDecimal("1000.9");
    try {
        mc = new MathContext(0, RoundingMode.FLOOR);
        res = a.pow(-1, mc);
        fail("ArithmeticException is not thrown for negative exponent and precision = 0");
    } catch (ArithmeticException e) {
    // expected
    }
    a = new BigDecimal("000.0001");
    try {
        mc = new MathContext(0, RoundingMode.FLOOR);
        res = a.pow(-1, mc);
        fail("ArithmeticException is not thrown for negative exponent and precision = 0");
    } catch (ArithmeticException e) {
    // expected
    }
    a = new BigDecimal("1E-400");
    mc = new MathContext(4, RoundingMode.UNNECESSARY);
    res = a.pow(-1, mc);
    assertEquals("incorrect value", "1E+400", res.toString());
//        Doesn't succeed against JDK of Sun!:
//        mc = new MathContext(3, RoundingMode.FLOOR);
//        a = new BigDecimal("100.9");
//        assertEquals("incorrect value", "1.00E+2000",
//                a.round(mc).pow(1000).round(mc).toString());
//        res = a.pow(1000).round(mc);
//        res = a.pow(1000, mc);
//        assertEquals("incorrect value", "7.783E+2003", res.toString());
}
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