Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6337226

BigDecimal.divideToIntegralValue(BigDecimal, MathContext) does not behave to spec

XMLWordPrintable

    • b63
    • other
    • windows_xp

        OPERATING SYSTEM(S):
        Windows XP

        FULL JDK VERSION(S):
        java version "1.5.0_05"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_05-b05)
        Java HotSpot(TM) Client VM (build 1.5.0_05-b05, mixed mode)

        DESCRIPTION:
        The method divideToIntegralValue(BigDecimal, MathContext) does not behave
        to spec in certain circumstances.
          From the API: "Throws ArithmeticException if mc.precision > 0 and the
        result requires a precision of more than mc.precision digits."
        This does not occur with mc.precision=1 and with operands such as: 11 by 1,
        22 by 2, 33 by 3 etc...

        The following test case should throw an exception, since 33/3 = 11. 11 can
        only be represented with a precision of at least 2, but we are setting the
        required precision to 1.

        import java.math.*;
        class d96029 {
                     public static void main (String [] args){
                                 java.math.BigDecimal jmLHS = new
        java.math.BigDecimal("33");
                                 java.math.BigDecimal jmRHS = new
        java.math.BigDecimal("3");
                                 System.out.println("About to divide 33 by 3 to
        integral value with max precision of 1.");
                                 java.math.BigDecimal result =
        jmLHS.divideToIntegralValue(
                                                         jmRHS, new
        java.math.MathContext(1, RoundingMode.UP));
                                 System.out.println("Should have thrown execption
        since 11 requires precision of 2. Result:");
                                 System.out.println(result);
                     }
        }

        Actual output is:
        About to divide 33 by 3 to integral value with max precision of 1.
        Should have thrown execption since 11 requires precision of 2. Result:
        1E+1

              darcy Joe Darcy
              elarsen Erik Larsen (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: