Search in sources :

Example 56 with MathContext

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

the class BigDecimalArithmeticTest method testRemainderMathContextHALF_DOWN.

/**
     * remainder(BigDecimal, MathContext)
     */
public void testRemainderMathContextHALF_DOWN() {
    String a = "3736186567876876578956958765675671119238118911893939591735";
    int aScale = -45;
    String b = "134432345432345748766876876723342238476237823787879183470";
    int bScale = 10;
    int precision = 75;
    RoundingMode rm = RoundingMode.HALF_DOWN;
    MathContext mc = new MathContext(precision, rm);
    String res = "1149310942946292909508821656680979993738625937.2065885780";
    int resScale = 10;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
    BigDecimal result = aNumber.remainder(bNumber, mc);
    assertEquals("incorrect quotient value", res, result.toString());
    assertEquals("incorrect quotient scale", resScale, result.scale());
}
Also used : RoundingMode(java.math.RoundingMode) BigInteger(java.math.BigInteger) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 57 with MathContext

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

the class BigDecimalArithmeticTest method testSubtractMathContextDiffScaleNegPos.

/**
     * Subtract two numbers of different scales using MathContext;
     *  the first is negative
     */
public void testSubtractMathContextDiffScaleNegPos() {
    String a = "986798656676789766678767876078779810457634781384756794987";
    int aScale = -15;
    String b = "747233429293018787918347987234564568";
    int bScale = 40;
    String c = "9.867986566767897666787678760787798104576347813847567949870000000000000E+71";
    int cScale = -2;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
    MathContext mc = new MathContext(70, RoundingMode.HALF_DOWN);
    BigDecimal result = aNumber.subtract(bNumber, mc);
    assertEquals("incorrect value", c, result.toString());
    assertEquals("incorrect scale", cScale, result.scale());
}
Also used : BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext)

Example 58 with MathContext

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

the class BigDecimalArithmeticTest method testMultiplyMathContextScalePosPos.

/**
     * Multiply two numbers of positive scales using MathContext
     */
public void testMultiplyMathContextScalePosPos() {
    String a = "97665696756578755423325476545428779810457634781384756794987";
    int aScale = -25;
    String b = "87656965586786097685674786576598865";
    int bScale = 10;
    String c = "8.561078619600910561431314228543672720908E+108";
    int cScale = -69;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
    MathContext mc = new MathContext(40, RoundingMode.HALF_DOWN);
    BigDecimal result = aNumber.multiply(bNumber, mc);
    assertEquals("incorrect value", c, result.toString());
    assertEquals("incorrect scale", cScale, result.scale());
}
Also used : BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext)

Example 59 with MathContext

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

the class BigDecimalArithmeticTest method testAddMathContextEqualScaleNegNeg.

/**
     * Add two numbers of equal negative scales using MathContext
     */
public void testAddMathContextEqualScaleNegNeg() {
    String a = "1231212478987482988429808779810457634781384756794987";
    int aScale = -10;
    String b = "747233429293018787918347987234564568";
    int bScale = -10;
    String c = "1.2312E+61";
    int cScale = -57;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
    MathContext mc = new MathContext(5, RoundingMode.FLOOR);
    BigDecimal result = aNumber.add(bNumber, mc);
    assertEquals("incorrect value ", c, result.toString());
    assertEquals("incorrect scale", cScale, result.scale());
}
Also used : BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext)

Example 60 with MathContext

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

the class BigDecimalArithmeticTest method testRoundMathContextPrecision0.

/**
     * round(BigDecimal, MathContext) when precision = 0
     */
public void testRoundMathContextPrecision0() {
    String a = "3736186567876876578956958765675671119238118911893939591735";
    int aScale = 45;
    int precision = 0;
    RoundingMode rm = RoundingMode.HALF_UP;
    MathContext mc = new MathContext(precision, rm);
    String res = "3736186567876.876578956958765675671119238118911893939591735";
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal result = aNumber.round(mc);
    assertEquals("incorrect quotient value", res, result.toString());
    assertEquals("incorrect quotient scale", aScale, result.scale());
}
Also used : RoundingMode(java.math.RoundingMode) BigInteger(java.math.BigInteger) 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