Search in sources :

Example 41 with MathContext

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

the class OldBigDecimalArithmeticTest method testDivideToIntegralValueMathContextNonTrivial.

public void testDivideToIntegralValueMathContextNonTrivial() {
    MathContext mc;
    BigDecimal a, b, res;
    a = new BigDecimal("92345678901234567.8");
    b = new BigDecimal("43");
    res = a.multiply(b);
    assertEquals("incorrect value", "3970864192753086415.4", res.toString());
    mc = new MathContext(20, RoundingMode.DOWN);
    a = new BigDecimal("3970864192753086415.4");
    b = new BigDecimal("92345678901234567.8");
    b = new BigDecimal("92345678901234567.8001");
    assertEquals("incorrect value", "43", a.round(mc).divideToIntegralValue(b.round(mc)).toString());
    res = a.divideToIntegralValue(b, mc);
    assertEquals("incorrect value", "42", res.toString());
    //        mc = new MathContext(1, RoundingMode.DOWN);
    //        res = a.divideToIntegralValue(b, mc);
    //        assertEquals("incorrect value", "42", res.toString());
    mc = new MathContext(17, RoundingMode.FLOOR);
    a = new BigDecimal("518518513851851830");
    b = new BigDecimal("12345678901234567.9");
    assertEquals("incorrect value", "42", a.round(mc).divideToIntegralValue(b.round(mc)).toString());
    res = a.divideToIntegralValue(b, mc);
    assertEquals("incorrect value", "41", res.toString());
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 42 with MathContext

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

the class OldBigDecimalConstructorsTest method testConstrDoubleMathContext_AndroidFailure.

public void testConstrDoubleMathContext_AndroidFailure() {
    BigDecimal bd;
    MathContext mc;
    mc = new MathContext(11, RoundingMode.HALF_EVEN);
    //
    // VERY FUNNY:
    // This works:
    bd = new BigDecimal("123456789.125", mc);
    assertEquals("incorrect value", "123456789.12", bd.toString());
    // But this doesn't:
    bd = new BigDecimal(123456789.125, mc);
    assertEquals("incorrect value", "123456789.12", bd.toString());
    bd = new BigDecimal(-123456789.125, mc);
    assertEquals("incorrect value", "-123456789.12", bd.toString());
    // AND HERE, TOO:
    mc = new MathContext(11, RoundingMode.HALF_UP);
    bd = new BigDecimal(123456789.125, mc);
    assertEquals("incorrect value", "123456789.13", bd.toString());
    bd = new BigDecimal(-123456789.125, mc);
    assertEquals("incorrect value", "-123456789.13", bd.toString());
    mc = new MathContext(11, RoundingMode.HALF_DOWN);
    //
    // SAME HERE:
    // This works:
    bd = new BigDecimal("123456789.125", mc);
    assertEquals("incorrect value", "123456789.12", bd.toString());
    // But this doesn't:
    bd = new BigDecimal(123456789.125, mc);
    assertEquals("incorrect value", "123456789.12", bd.toString());
    bd = new BigDecimal(123456789.125, mc);
    assertEquals("incorrect value", "123456789.12", bd.toString());
    bd = new BigDecimal(-123456789.125, mc);
    assertEquals("incorrect value", "-123456789.12", bd.toString());
}
Also used : BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext)

Example 43 with MathContext

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

the class OldBigDecimalConvertTest method test_ByteValueStringZero.

public void test_ByteValueStringZero() {
    String sNumber = "00000";
    int iNumber = 0;
    int iPresition = 0;
    MathContext mc = new MathContext(iPresition, RoundingMode.HALF_UP);
    BigDecimal bdNumber = new BigDecimal(sNumber, mc);
    byte bNumber = bdNumber.byteValue();
    assertTrue("incorrect byteValue", iNumber == bNumber);
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 44 with MathContext

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

the class OldBigDecimalConvertTest method test_ByteValueCharZero.

public void test_ByteValueCharZero() {
    char[] cNumber = { '-', '0', '.', '0' };
    int iNumber = 0;
    int iPresition = 0;
    MathContext mc = new MathContext(iPresition, RoundingMode.HALF_UP);
    BigDecimal bdNumber = new BigDecimal(cNumber, mc);
    byte bNumber = bdNumber.byteValue();
    assertTrue("incorrect byteValue", iNumber == bNumber);
}
Also used : MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 45 with MathContext

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

the class OldBigDecimalConvertTest method test_ByteValueExactCharZero.

public void test_ByteValueExactCharZero() {
    char[] cNumber = { '-', '0', '.', '0' };
    int iNumber = 0;
    int iPresition = 5;
    MathContext mc = new MathContext(iPresition, RoundingMode.HALF_DOWN);
    BigDecimal bdNumber = new BigDecimal(cNumber, mc);
    byte bNumber = bdNumber.byteValueExact();
    assertTrue("incorrect byteValueExact", iNumber == bNumber);
}
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