changeset 459:3857c2791784

6850606: Regression from JDK 1.6.0_12 Summary: The returned result from multiply should be constructed by using valueOf to take care of the INFLATED case. Reviewed-by: darcy
author aph
date Wed, 03 Nov 2010 13:21:29 +0000
parents cadc64fc2282
children c2b73679ba94
files src/share/classes/java/math/BigDecimal.java test/java/math/BigDecimal/MultiplyTests.java
diffstat 2 files changed, 102 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/math/BigDecimal.java	Wed Nov 03 12:43:51 2010 +0000
+++ b/src/share/classes/java/math/BigDecimal.java	Wed Nov 03 13:21:29 2010 +0000
@@ -1101,7 +1101,7 @@
             // See "Hacker's Delight" section 2-12 for explanation of
             // the overflow test.
             if ( (((sum ^ xs) & (sum ^ ys))) >= 0L) // not overflowed
-                return new BigDecimal(null, sum, rscale, 0);
+                return BigDecimal.valueOf(sum, rscale);
         }
         if (fst == null)
             fst = BigInteger.valueOf(xs);
@@ -1311,9 +1311,9 @@
              * would occur since division is expensive on most CPUs.
              */
             long product = x * y;
-            int prec = this.precision() + multiplicand.precision();
+            long prec = this.precision() + multiplicand.precision();
             if (prec < 19 || (prec < 21 && (y == 0 || product / y == x)))
-                return new BigDecimal(null, product, productScale, 0);
+                return BigDecimal.valueOf(product, productScale);
             return new BigDecimal(BigInteger.valueOf(x).multiply(y), INFLATED,
                                   productScale, 0);
         }
@@ -1584,7 +1584,7 @@
             return (preferredScale >= 0 &&
                     preferredScale < ZERO_SCALED_BY.length) ?
                 ZERO_SCALED_BY[preferredScale] :
-                new BigDecimal(null, 0, preferredScale, 1);
+                BigDecimal.valueOf(0, preferredScale);
         else {
             this.inflate();
             divisor.inflate();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/math/BigDecimal/MultiplyTests.java	Wed Nov 03 13:21:29 2010 +0000
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 6850606
+ * @summary Test BigDecimal.multiply(BigDecimal)
+ * @author xlu
+ */
+
+import java.math.*;
+import static java.math.BigDecimal.*;
+
+public class MultiplyTests {
+
+    private static int multiplyTests() {
+        int failures = 0;
+
+        BigDecimal[] bd1 = {
+            new BigDecimal("123456789"),
+            new BigDecimal("1234567898"),
+            new BigDecimal("12345678987")
+        };
+
+        BigDecimal[] bd2 = {
+            new BigDecimal("987654321"),
+            new BigDecimal("8987654321"),
+            new BigDecimal("78987654321")
+        };
+
+        // Two dimensonal array recording bd1[i] * bd2[j] &
+        // 0 <= i <= 2 && 0 <= j <= 2;
+        BigDecimal[][] expectedResults = {
+            {new BigDecimal("121932631112635269"),
+             new BigDecimal("1109586943112635269"),
+             new BigDecimal("9751562173112635269")
+            },
+            { new BigDecimal("1219326319027587258"),
+              new BigDecimal("11095869503027587258"),
+              new BigDecimal("97515622363027587258")
+            },
+            { new BigDecimal("12193263197189452827"),
+              new BigDecimal("110958695093189452827"),
+              new BigDecimal("975156224183189452827")
+            }
+        };
+
+        for (int i = 0; i < bd1.length; i++) {
+            for (int j = 0; j < bd2.length; j++) {
+                if (!bd1[i].multiply(bd2[j]).equals(expectedResults[i][j])) {
+                    failures++;
+                }
+            }
+        }
+
+        BigDecimal x = BigDecimal.valueOf(8L, 1);
+        BigDecimal xPower = BigDecimal.valueOf(-1L);
+        try {
+            for (int i = 0; i < 100; i++) {
+                xPower = xPower.multiply(x);
+            }
+        } catch (Exception ex) {
+            failures++;
+        }
+        return failures;
+    }
+
+    public static void main(String[] args) {
+        int failures = 0;
+
+        failures += multiplyTests();
+
+        if (failures > 0) {
+            throw new RuntimeException("Incurred " + failures +
+                                       " failures while testing multiply.");
+        }
+    }
+}