Commit 69848904 by Warren Levy Committed by Warren Levy

re PR libgcj/1596 (setScale() missing from java.math.BigDecimal)

	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.

From-SVN: r38861
parent 534808a3
2001-01-10 Warren Levy <warrenl@redhat.com>
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.
2001-01-09 Oskar Liljeblad <osk@hem.passagen.se> 2001-01-09 Oskar Liljeblad <osk@hem.passagen.se>
Fix for PR libgcj/1338: Fix for PR libgcj/1338:
......
/* java.math.BigDecimal -- Arbitrary precision decimals. /* 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. This file is part of GNU Classpath.
...@@ -133,16 +133,27 @@ public class BigDecimal extends Number implements Comparable { ...@@ -133,16 +133,27 @@ public class BigDecimal extends Number implements Comparable {
throw throw
new IllegalArgumentException("illegal rounding mode: " + roundingMode); new IllegalArgumentException("illegal rounding mode: " + roundingMode);
if (scale < 0) if (newScale < 0)
throw new ArithmeticException ("scale is negative: " + scale); throw new ArithmeticException ("scale is negative: " + newScale);
if (intVal.signum () == 0) // handle special case of 0.0/0.0 if (intVal.signum () == 0) // handle special case of 0.0/0.0
return ZERO; return ZERO;
BigInteger dividend = intVal.multiply (BigInteger.valueOf (10).pow // Ensure that pow gets a non-negative value.
(newScale + 1 - (scale - val.scale))); 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("int: " + parts[0]);
// System.out.println("rem: " + parts[1]); // System.out.println("rem: " + parts[1]);
...@@ -347,4 +358,15 @@ public class BigDecimal extends Number implements Comparable { ...@@ -347,4 +358,15 @@ public class BigDecimal extends Number implements Comparable {
{ {
return Double.valueOf(toString()).doubleValue(); 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);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment