Java Code Examples for java.math.MathContext#DECIMAL32

The following examples show how to use java.math.MathContext#DECIMAL32 . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultValueConfiguration.java    From jtwig-core with Apache License 2.0 6 votes vote down vote up
public DefaultValueConfiguration() {
    super(MathContext.DECIMAL32, RoundingMode.HALF_UP, Arrays.<Converter<Boolean>>asList(
            new BooleanConverter()
    ), Arrays.<Converter<BigDecimal>>asList(
            new BigDecimalConverter()
    ), Arrays.asList(
            new NullConverter<WrappedCollection>(),
            new ArrayToCollectionConverter(),
            new IterableToCollectionConverter(),
            new MapToCollectionConverter()
    ), Arrays.asList(
            new NullConverter<Character>(),
            new CharConverter()
    ), new DefaultValueComparator(),
            new DefaultStringConverter());
}
 
Example 2
Source File: BigDecimalConstructorsTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * new BigDecimal(char[] value, int offset, int len, MathContext mc); 
 */
public void testConstrCharIntIntMathContext() {
    char value[] = {'-', '1', '2', '3', '8', '0', '.', '4', '7', '3', '8', 'E', '-', '4', '2', '3'};
    int offset = 3;
    int len = 12;
    int precision = 4;
    RoundingMode rm = RoundingMode.CEILING;
    MathContext mc = new MathContext(precision, rm);
    BigDecimal result = new BigDecimal(value, offset, len, mc);
    String res = "3.805E-40";
    int resScale = 43;
    assertEquals("incorrect value", res, result.toString());
    assertEquals("incorrect scale", resScale, result.scale());
    
    try {
        // Regression for HARMONY-783
        new BigDecimal(new char[] {}, 0, 0, MathContext.DECIMAL32);
        fail("NumberFormatException has not been thrown");
    } catch (NumberFormatException e) {
    }
 }
 
Example 3
Source File: RejectBigDecimalDivision.java    From AVM with MIT License 5 votes vote down vote up
public BigDecimal divide() {
    BigDecimal dividend = new BigDecimal("3.14159");
    BigDecimal divisor = new BigDecimal("1.01");
    MathContext context = MathContext.DECIMAL32;
    BigDecimal pair[] = dividend.divideAndRemainder(divisor, context);
    return pair[0];
}
 
Example 4
Source File: BigDecimalConstructorsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * new BigDecimal(char[] value, MathContext mc);
 */
public void testConstrCharMathContext() {
    try {
        // Regression for HARMONY-783
        new BigDecimal(new char[] {}, MathContext.DECIMAL32);
        fail("NumberFormatException has not been thrown");
    } catch (NumberFormatException e) {
    }
}
 
Example 5
Source File: SquareRootOfBigIntegerExample.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {

    SquareRootOfBigIntegerExample SquareRootOfBigIntegerExample =
            new SquareRootOfBigIntegerExample();
    String n = "";

    MathContext mc = new MathContext(0, RoundingMode.DOWN);
    mc = MathContext.DECIMAL32;

    BigInteger my2P100000 = new BigInteger("0");
    BigInteger two = new BigInteger("2");
    BigInteger one = new BigInteger("1");

    my2P100000 = two.shiftLeft(2000 - 1);

    System.out.println("2^2000 --  Step 1");
    System.out.println("Value of 2^2,000 " + my2P100000);
    System.out.println("");
    System.out.println("Finding the Square Root of 2^2000");

    String mys = my2P100000 + "";
    n = (mys);
    int firsttime = 0;

    BigDecimal myNumber = new BigDecimal(n);
    BigDecimal g = new BigDecimal("1");
    BigDecimal my2 = new BigDecimal("2");
    BigDecimal epsilon = new BigDecimal("0.0000000001");

    BigDecimal nByg = myNumber.divide(g, 9, BigDecimal.ROUND_FLOOR);

    //Get the value of n/g
    BigDecimal nBygPlusg = nByg.add(g);

    //Get the value of "n/g + g
    BigDecimal nBygPlusgHalf = nBygPlusg.divide(my2, 9, BigDecimal.ROUND_FLOOR);

    //Get the value of (n/g + g)/2
    BigDecimal saveg = nBygPlusgHalf;
    firsttime = 99;

    do {
      g = nBygPlusgHalf;
      nByg = myNumber.divide(g, 9, BigDecimal.ROUND_FLOOR);
      nBygPlusg = nByg.add(g);
      nBygPlusgHalf = nBygPlusg.divide(my2, 9, BigDecimal.ROUND_FLOOR);
      BigDecimal savegdiff = saveg.subtract(nBygPlusgHalf);

      if (savegdiff.compareTo(epsilon) == -1) {
        firsttime = 0;
      } else {
        saveg = nBygPlusgHalf;
      }

    } while (firsttime > 1);

    System.out.println(
            "For " + mys + "\nLength: " + mys.length() + "\nThe Square Root is " + saveg);
  }
 
Example 6
Source File: SquareRootOfBigIntegerExample.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) {

        SquareRootOfBigIntegerExample SquareRootOfBigIntegerExample =
                new SquareRootOfBigIntegerExample();
        String n = "";

        MathContext mc = new MathContext(0, RoundingMode.DOWN);
        mc = MathContext.DECIMAL32;

        BigInteger my2P100000 = new BigInteger("0");
        BigInteger two = new BigInteger("2");
        BigInteger one = new BigInteger("1");

        my2P100000 = two.shiftLeft(2000 - 1);

        System.out.println("2^2000 --  Step 1");
        System.out.println("Value of 2^2,000 " + my2P100000);
        System.out.println("");
        System.out.println("Finding the Square Root of 2^2000");

        String mys = my2P100000 + "";
        n = (mys);
        int firsttime = 0;

        BigDecimal myNumber = new BigDecimal(n);
        BigDecimal g = new BigDecimal("1");
        BigDecimal my2 = new BigDecimal("2");
        BigDecimal epsilon = new BigDecimal("0.0000000001");

        BigDecimal nByg = myNumber.divide(g, 9, BigDecimal.ROUND_FLOOR);

        //Get the value of n/g
        BigDecimal nBygPlusg = nByg.add(g);

        //Get the value of "n/g + g
        BigDecimal nBygPlusgHalf = nBygPlusg.divide(my2, 9, BigDecimal.ROUND_FLOOR);

        //Get the value of (n/g + g)/2
        BigDecimal saveg = nBygPlusgHalf;
        firsttime = 99;

        do {
            g = nBygPlusgHalf;
            nByg = myNumber.divide(g, 9, BigDecimal.ROUND_FLOOR);
            nBygPlusg = nByg.add(g);
            nBygPlusgHalf = nBygPlusg.divide(my2, 9, BigDecimal.ROUND_FLOOR);
            BigDecimal savegdiff = saveg.subtract(nBygPlusgHalf);

            if (savegdiff.compareTo(epsilon) == -1) {
                firsttime = 0;
            } else {
                saveg = nBygPlusgHalf;
            }

        } while (firsttime > 1);

        System.out.println(
                "For " + mys + "\nLength: " + mys.length() + "\nThe Square Root is " + saveg);
    }