Search in sources :

Example 11 with MathContext

use of java.math.MathContext in project j2objc by google.

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 12 with MathContext

use of java.math.MathContext in project j2objc by google.

the class BigDecimalArithmeticTest method testAddMathContextEqualScalePosPos.

/**
     * Add two numbers of equal positive scales using MathContext
     */
public void testAddMathContextEqualScalePosPos() {
    String a = "1231212478987482988429808779810457634781384756794987";
    int aScale = 10;
    String b = "747233429293018787918347987234564568";
    int bScale = 10;
    String c = "1.2313E+41";
    int cScale = -37;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
    MathContext mc = new MathContext(5, RoundingMode.UP);
    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 13 with MathContext

use of java.math.MathContext in project j2objc by google.

the class BigDecimalArithmeticTest method testSubtractMathContextEqualScalePosPos.

/**
     * Subtract two numbers of equal positive scales using MathContext
     */
public void testSubtractMathContextEqualScalePosPos() {
    String a = "1231212478987482988429808779810457634781384756794987";
    int aScale = 10;
    String b = "747233429293018787918347987234564568";
    int bScale = 10;
    String c = "1.23121247898749E+41";
    int cScale = -27;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
    MathContext mc = new MathContext(15, RoundingMode.CEILING);
    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 14 with MathContext

use of java.math.MathContext in project j2objc by google.

the class BigDecimalArithmeticTest method testDivideAndRemainderMathContextDOWN.

/**
     * divideAndRemainder(BigDecimal, MathContext)
     */
public void testDivideAndRemainderMathContextDOWN() {
    String a = "3736186567876876578956958765675671119238118911893939591735";
    int aScale = 45;
    String b = "134432345432345748766876876723342238476237823787879183470";
    int bScale = 20;
    int precision = 15;
    RoundingMode rm = RoundingMode.DOWN;
    MathContext mc = new MathContext(precision, rm);
    String res = "0E-25";
    int resScale = 25;
    String rem = "3736186567876.876578956958765675671119238118911893939591735";
    int remScale = 45;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
    BigDecimal[] result = aNumber.divideAndRemainder(bNumber, mc);
    assertEquals("incorrect quotient value", res, result[0].toString());
    assertEquals("incorrect quotient scale", resScale, result[0].scale());
    assertEquals("incorrect remainder value", rem, result[1].toString());
    assertEquals("incorrect remainder scale", remScale, result[1].scale());
}
Also used : RoundingMode(java.math.RoundingMode) BigInteger(java.math.BigInteger) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 15 with MathContext

use of java.math.MathContext in project j2objc by google.

the class BigDecimalArithmeticTest method testDivideBigDecimalScaleMathContextUP.

/**
     * divide(BigDecimal, MathContext)
     */
public void testDivideBigDecimalScaleMathContextUP() {
    String a = "3736186567876876578956958765675671119238118911893939591735";
    int aScale = 15;
    String b = "748766876876723342238476237823787879183470";
    int bScale = 10;
    int precision = 21;
    RoundingMode rm = RoundingMode.UP;
    MathContext mc = new MathContext(precision, rm);
    String c = "49897861180.2562512996";
    int resScale = 10;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
    BigDecimal result = aNumber.divide(bNumber, mc);
    assertEquals("incorrect value", c, result.toString());
    assertEquals("incorrect scale", resScale, 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