Search in sources :

Example 6 with MathContext

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

the class BigDecimalConstructorsTest method testConstrLongMathContext.

/**
     * new BigDecimal(long, MathContext)
     */
public void testConstrLongMathContext() {
    long a = 4576578677732546982L;
    int precision = 5;
    RoundingMode rm = RoundingMode.CEILING;
    MathContext mc = new MathContext(precision, rm);
    String res = "45766";
    int resScale = -14;
    BigDecimal result = new BigDecimal(a, mc);
    assertEquals("incorrect value", res, result.unscaledValue().toString());
    assertEquals("incorrect scale", resScale, result.scale());
}
Also used : RoundingMode(java.math.RoundingMode) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 7 with MathContext

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

the class BigDecimalCompareTest method testAbsMathContextNeg.

/**
     * Abs(MathContext) of a negative BigDecimal
     */
public void testAbsMathContextNeg() {
    String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21";
    BigDecimal aNumber = new BigDecimal(a);
    int precision = 15;
    RoundingMode rm = RoundingMode.HALF_DOWN;
    MathContext mc = new MathContext(precision, rm);
    String result = "1.23809648392385E+53";
    int resScale = -39;
    BigDecimal res = aNumber.abs(mc);
    assertEquals("incorrect value", result, res.toString());
    assertEquals("incorrect scale", resScale, res.scale());
}
Also used : RoundingMode(java.math.RoundingMode) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext)

Example 8 with MathContext

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

the class BigDecimalCompareTest method testPlusMathContextPositive.

/**
     * plus(MathContext) for a positive BigDecimal
     */
public void testPlusMathContextPositive() {
    String a = "92948782094488478231212478987482988429808779810457634781384756794987";
    int aScale = 41;
    int precision = 37;
    RoundingMode rm = RoundingMode.FLOOR;
    MathContext mc = new MathContext(precision, rm);
    String c = "929487820944884782312124789.8748298842";
    int cScale = 10;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal res = aNumber.plus(mc);
    assertEquals("incorrect value", c, res.toString());
    assertEquals("incorrect scale", cScale, res.scale());
}
Also used : RoundingMode(java.math.RoundingMode) BigInteger(java.math.BigInteger) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 9 with MathContext

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

the class BigDecimalCompareTest method testNegateMathContextNegative.

/**
     * negate(MathContext) for a negative BigDecimal
     */
public void testNegateMathContextNegative() {
    String a = "-92948782094488478231212478987482988429808779810457634781384756794987";
    int aScale = 49;
    int precision = 46;
    RoundingMode rm = RoundingMode.CEILING;
    MathContext mc = new MathContext(precision, rm);
    String c = "9294878209448847823.121247898748298842980877981";
    int cScale = 27;
    BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
    BigDecimal res = aNumber.negate(mc);
    assertEquals("incorrect value", c, res.toString());
    assertEquals("incorrect scale", cScale, res.scale());
}
Also used : RoundingMode(java.math.RoundingMode) BigInteger(java.math.BigInteger) MathContext(java.math.MathContext) BigDecimal(java.math.BigDecimal)

Example 10 with MathContext

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

the class BigDecimalArithmeticTest method testDivideBigDecimalScaleMathContextHALF_DOWN.

/**
     * divide(BigDecimal, MathContext)
     */
public void testDivideBigDecimalScaleMathContextHALF_DOWN() {
    String a = "3736186567876876578956958765675671119238118911893939591735";
    int aScale = 45;
    String b = "134432345432345748766876876723342238476237823787879183470";
    int bScale = 70;
    int precision = 21;
    RoundingMode rm = RoundingMode.HALF_DOWN;
    MathContext mc = new MathContext(precision, rm);
    String c = "2.77923185514690367475E+26";
    int resScale = -6;
    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