JDK-6458944 : BigDecimal.setScale(int, int) no longer throws ArithmeticException
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.math
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: linux
  • CPU: x86
  • Submitted: 2006-08-10
  • Updated: 2011-02-16
  • Resolved: 2006-08-10
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_07"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-b03)
Java HotSpot(TM) Client VM (build 1.5.0_07-b03, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Fedora Core 5

A DESCRIPTION OF THE PROBLEM :
In 1.5, BigDecimal.setScale(int, int) no longer throws ArithmeticException if the scale parameter is smaller then zero.

It should do so according to the spec, and does so in 1.4.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Testcase given.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
An ArithmeticException should be thrown. (Or the spec changed)
ACTUAL -
Returned the results of a division with 10^-scale.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
(new BigDecimal(1.0)).setScale(-10, BigDecimal.ROUND_DOWN);

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Check the parameters yourself before calling setScale.

Comments
EVALUATION The specification for setScale(int, int) in JDK 1.4.2 includes Throws: ArithmeticException - scale is negative, or roundingMode==ROUND_UNNECESSARY and the specified scaling operation would require rounding. The corresponding specification in JDK 5 and latest includes Throws: ArithmeticException - if roundingMode==ROUND_UNNECESSARY and the specified scaling operation would require rounding. The specification of setScale was changed to not throw an exception on a negative scale because the set of representable values was expanded to include values with negative scales. Closing as not a bug.
10-08-2006