aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/math
diff options
context:
space:
mode:
authorWarren Levy <warrenl@redhat.com>2001-01-10 09:38:08 +0000
committerWarren Levy <warrenl@redhat.com>2001-01-10 09:38:08 +0000
commit479cd50afe40be86a94ebc86077b1564ed74c1e7 (patch)
tree232892def7d8356240f760851dd1f02e6943744d /libjava/java/math
parentde1c702865bca2357e99d720f462087d1cc5f965 (diff)
Fix for PR libgcj/1596:
* java/math/BigDecimal.java (divide): Check newScale for validity. Ensure that BigInteger.pow() is called with a non-negative value. (setScale (int)): New public method. (setScale (int,int)): New public method. git-svn-id: https://gcc.gnu.org/svn/gcc/trunk@38861 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libjava/java/math')
-rw-r--r--libjava/java/math/BigDecimal.java34
1 files changed, 28 insertions, 6 deletions
diff --git a/libjava/java/math/BigDecimal.java b/libjava/java/math/BigDecimal.java
index 6844b24961a..007f3a32407 100644
--- a/libjava/java/math/BigDecimal.java
+++ b/libjava/java/math/BigDecimal.java
@@ -1,5 +1,5 @@
/* java.math.BigDecimal -- Arbitrary precision decimals.
- Copyright (C) 1999, 2000 Free Software Foundation, Inc.
+ Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -133,16 +133,27 @@ public class BigDecimal extends Number implements Comparable {
throw
new IllegalArgumentException("illegal rounding mode: " + roundingMode);
- if (scale < 0)
- throw new ArithmeticException ("scale is negative: " + scale);
+ if (newScale < 0)
+ throw new ArithmeticException ("scale is negative: " + newScale);
if (intVal.signum () == 0) // handle special case of 0.0/0.0
return ZERO;
- BigInteger dividend = intVal.multiply (BigInteger.valueOf (10).pow
- (newScale + 1 - (scale - val.scale)));
+ // Ensure that pow gets a non-negative value.
+ int valScale = val.scale;
+ BigInteger valIntVal = val.intVal;
+ int power = newScale + 1 - (scale - val.scale);
+ if (power < 0)
+ {
+ // Effectively increase the scale of val to avoid an
+ // IllegalArgumentException for a negative power.
+ valIntVal = valIntVal.multiply (BigInteger.valueOf (10).pow (-power));
+ power = 0;
+ }
+
+ BigInteger dividend = intVal.multiply (BigInteger.valueOf (10).pow (power));
- BigInteger parts[] = dividend.divideAndRemainder (val.intVal);
+ BigInteger parts[] = dividend.divideAndRemainder (valIntVal);
// System.out.println("int: " + parts[0]);
// System.out.println("rem: " + parts[1]);
@@ -347,4 +358,15 @@ public class BigDecimal extends Number implements Comparable {
{
return Double.valueOf(toString()).doubleValue();
}
+
+ public BigDecimal setScale (int scale) throws ArithmeticException
+ {
+ return setScale (scale, ROUND_UNNECESSARY);
+ }
+
+ public BigDecimal setScale (int scale, int roundingMode)
+ throws ArithmeticException, IllegalArgumentException
+ {
+ return divide (ONE, scale, roundingMode);
+ }
}