United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6819773 BigDecimal.divide(BigDecimal,int,int) throws ArrayIndexOutofBoundsException under some circumstances
JDK-6819773 : BigDecimal.divide(BigDecimal,int,int) throws ArrayIndexOutofBoundsException under some circumstances

Details
Type:
Bug
Submit Date:
2009-03-19
Status:
Resolved
Updated Date:
2011-02-15
Project Name:
JDK
Resolved Date:
2009-03-28
Component:
core-libs
OS:
generic
Sub-Component:
java.math
CPU:
generic
Priority:
P2
Resolution:
Fixed
Affected Versions:
6u14
Fixed Versions:
6u14 (b04)

Related Reports
Duplicate:
Relates:
Relates:

Sub Tasks

Description
java.math.BigDecimal.divide(BigDecimal divisor, int scale, int roundingMode) 
throws unspecified ArrayIndexOutOfBoundsException
when roundingMode equals BigDecimal.ROUND_HALF_EVEN


How to reproduce:
-----------------

To reproduce this bug run the following BigDecimalTest

NOTE: the code is copied from Apache license Harmony test.

-------------------BigDecimalTest.java--------
import java.math.*;

import java.math.*;

public class BigDecimalTest {
    public static void main(String[] args) {
        String dec1 = "-37361671119238118911893939591735";
        int scale1 = 10;
        String dec2 = "74723342238476237823787879183470";
        int scale2 = 15;
        int resScale = -5;
        BigDecimal decN1 = new BigDecimal(new BigInteger(dec1), scale1);
        BigDecimal decN2 = new BigDecimal(new BigInteger(dec2), scale2);
        BigDecimal result;
        int round = -1;
        String sRound = "";
        try {
            round = BigDecimal.ROUND_HALF_UP;
            sRound = "BigDecimal.ROUND_HALF_UP";
            result = decN1.divide(decN2, resScale, round);
            System.out.println("divide with " + sRound + " passsed. result="
                + result);

            round = BigDecimal.ROUND_HALF_DOWN;
            sRound = "BigDecimal.ROUND_HALF_DOWN";
            result = decN1.divide(decN2, resScale, round);
            System.out.println("divide with " + sRound + " passsed. result="
                + result);

            round = BigDecimal.ROUND_HALF_EVEN;
            sRound = "BigDecimal.ROUND_HALF_EVEN";
            result = decN1.divide(decN2, resScale, round);
            System.out.println("divide with " + sRound + " passsed. result="
                + result);

        } catch (Throwable e) {
            System.out.println("divide with " + sRound + " failed");
            System.out.println("Unexpected exception was thrown. " + round);
            e.printStackTrace(System.out);
        }
    }
}
------------

Diagnostics/output:
-------------------

Output on 6u14 b03 ia32:

java version "1.6.0_14-ea"
Java(TM) SE Runtime Environment (build 1.6.0_14-ea-b03)
Java HotSpot(TM) Client VM (build 14.0-b12, mixed mode, sharing)

divide with BigDecimal.ROUND_HALF_UP passsed. result=-1E+5
divide with BigDecimal.ROUND_HALF_DOWN passsed. result=0E+5
divide with BigDecimal.ROUND_HALF_EVEN failed
Unexpected exception was thrown. 6
java.lang.ArrayIndexOutOfBoundsException: -1
        at java.math.MutableBigInteger.isOdd(Unknown Source)
        at java.math.BigDecimal.divideAndRound(Unknown Source)
        at java.math.BigDecimal.divide(Unknown Source)
        at BigDecimalTest.main(BigDecimalTest.java:30)

                                    

Comments
EVALUATION

See comments section.
                                     
2009-03-19



Hardware and Software, Engineered to Work Together