More BD stuff.
2006-02-28 Anthony Balkissoon <[EMAIL PROTECTED]>
* java/math/BigDecimal.java:
(divide(BigDecimal, int, RoundingMode)): New method.
(divide(BigDecimal, RoundingMode)): Likewise.
(divide(BigDecimal, int, int)): Removed incorrect throwing of exception
when the new scale is < 0.
(setScale(int, RoundingMode)): New method.
(ulp): Likewise.
--Tony
Index: java/math/BigDecimal.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/math/BigDecimal.java,v
retrieving revision 1.17.2.13
diff -u -r1.17.2.13 BigDecimal.java
--- java/math/BigDecimal.java 27 Feb 2006 21:35:15 -0000 1.17.2.13
+++ java/math/BigDecimal.java 28 Feb 2006 20:28:12 -0000
@@ -670,7 +670,38 @@
{
return divide (val, scale, roundingMode);
}
+
+ /**
+ * Returns a BigDecimal whose value is (this / val), with the specified scale
+ * and rounding according to the RoundingMode
+ * @param val the divisor
+ * @param scale the scale of the BigDecimal returned
+ * @param roundingMode the rounding mode to use
+ * @return a BigDecimal whose value is approximately (this / val)
+ * @throws ArithmeticException if divisor is zero or the rounding mode is
+ * UNNECESSARY but the specified scale cannot represent the value exactly
+ * @since 1.5
+ */
+ public BigDecimal divide(BigDecimal val,
+ int scale, RoundingMode roundingMode)
+ {
+ return divide (val, scale, roundingMode.ordinal());
+ }
+ /**
+ * Returns a BigDecimal whose value is (this / val) rounded according to the
+ * RoundingMode
+ * @param val the divisor
+ * @param roundingMode the rounding mode to use
+ * @return a BigDecimal whose value is approximately (this / val)
+ * @throws ArithmeticException if divisor is zero or the rounding mode is
+ * UNNECESSARY but the specified scale cannot represent the value exactly
+ */
+ public BigDecimal divide (BigDecimal val, RoundingMode roundingMode)
+ {
+ return divide (val, scale, roundingMode.ordinal());
+ }
+
public BigDecimal divide(BigDecimal val, int newScale, int roundingMode)
throws ArithmeticException, IllegalArgumentException
{
@@ -678,9 +709,6 @@
throw
new IllegalArgumentException("illegal rounding mode: " + roundingMode);
- if (newScale < 0)
- throw new ArithmeticException ("scale is negative: " + newScale);
-
if (intVal.signum () == 0) // handle special case of 0.0/0.0
return newScale == 0 ? ZERO : new BigDecimal (ZERO.intVal, newScale);
@@ -1346,6 +1374,23 @@
}
/**
+ * Returns a BigDecimal whose value is the same as this BigDecimal but whose
+ * representation has a scale of <code>newScale</code>. If the scale is
+ * reduced then rounding may occur, according to the RoundingMode.
+ * @param newScale
+ * @param roundingMode
+ * @return a BigDecimal whose scale is as given, whose value is
+ * <code>this</code> with possible rounding
+ * @throws ArithmeticException if the rounding mode is UNNECESSARY but
+ * rounding is required
+ * @since 1.5
+ */
+ public BigDecimal setScale(int newScale, RoundingMode roundingMode)
+ {
+ return setScale(newScale, roundingMode.ordinal());
+ }
+
+ /**
* Returns a new BigDecimal constructed from the BigDecimal(String)
* constructor using the Double.toString(double) method to obtain
* the String.
@@ -1419,4 +1464,15 @@
result = result.round(mc);
return result;
}
+
+ /**
+ * Returns the size of a unit in the last place of this BigDecimal. This
+ * returns a BigDecimal with [unscaledValue, scale] = [1, this.scale()].
+ * @return the size of a unit in the last place of <code>this</code>.
+ * @since 1.5
+ */
+ public BigDecimal ulp()
+ {
+ return new BigDecimal(BigInteger.ONE, scale);
+ }
}