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

Unchecked scale manipulation in BigDecimal.dropDigits

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:
        In some cases, rounding can cause an unnoticed overflow of a BigDecimal's scale.
        This is caused by an unchecked manipulation of the scale in BigDecimal.dropDigits:
        - Minimal source code that demonstrates the problem
        import java.math.*;
        public class BDScaleOverFlow {
            public static void main(String[] args) {
                BigDecimal j = BigDecimal.valueOf(11, Integer.MIN_VALUE);
                System.out.format("%s (scale: %s)\n", j, j.scale());
                //The following should throw an exception since we would be
                //forcing the scale to fall below Integer.MIN_VALUE
                System.out.println("About to round to precision 1, expecting exception.");
                j = j.round(new MathContext(1));
                System.out.format("%s (scale: %s)", j, j.scale());
            }
        }
        - Exact text of any error messages
        Output:
        1.1E+2147483649 (scale: -2147483648)
        About to round to precision 1, expecting exception.
        1E-2147483647 (scale: 2147483647)
        The testcase should throw an exception.

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

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: