JDK-6337226 : BigDecimal.divideToIntegralValue(BigDecimal, MathContext) does not behave to spec
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.math
  • Affected Version: 5.0u5
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: other
  • Submitted: 2005-10-14
  • Updated: 2010-04-02
  • Resolved: 2005-12-03
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other JDK 6
5.0u8Fixed 6 b63Fixed
Description
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

Comments
SUGGESTED FIX ------- BigDecimal.java ------- 1659c1659,1661 < if (this.subtract(product).abs().compareTo(divisor.abs()) > 0) { --- > // If the quotient is the full integer value, > // |dividend-product| < |divisor|. > if (this.subtract(product).abs().compareTo(divisor.abs()) >= 0) {
28-11-2005

EVALUATION Should be fixed.
25-10-2005

WORK AROUND In BigDecimal.divideToIntegralValue(BigDecimal, MathContext), replace: if (this.subtract(product).abs().compareTo(divisor.abs()) >> 0) { by if (this.subtract(product).abs().compareTo(divisor.abs()) >>= 0) {
14-10-2005