diff options
author | Anthony Balkissoon <abalkiss@redhat.com> | 2006-02-27 19:49:07 +0000 |
---|---|---|
committer | Anthony Balkissoon <abalkiss@redhat.com> | 2006-02-27 19:49:07 +0000 |
commit | 962edfc667e39f42f9ae4dfb55c0ac0b4d61dce4 (patch) | |
tree | 810578596196aed6953133d3527afdd847226004 | |
parent | 710b04d41fbfd69cb7c0ca5faf2a6abf880ef4ce (diff) | |
download | classpath-962edfc667e39f42f9ae4dfb55c0ac0b4d61dce4.tar.gz |
2006-02-27 Anthony Balkissoon <abalkiss@redhat.com>
* java/math/BigDecimal.java: Added @throws clause to constructors.
(mathContext): Removed this unneeded field.
(BigDecimal(int, MathContext)): New constructor.
(BigDecimal(BigInteger, int, MathContext)): Likewise.
(multiply(BigDecimal, MathContext)): New method.
(negate(MathContext)): Likewise.
(plus(MathContext)): Likewise.
(numDigitsInLong): Fixed to properly handle negatives.
-rw-r--r-- | ChangeLog | 11 | ||||
-rw-r--r-- | java/math/BigDecimal.java | 103 |
2 files changed, 111 insertions, 3 deletions
@@ -1,3 +1,14 @@ +2006-02-27 Anthony Balkissoon <abalkiss@redhat.com> + + * java/math/BigDecimal.java: Added @throws clause to constructors. + (mathContext): Removed this unneeded field. + (BigDecimal(int, MathContext)): New constructor. + (BigDecimal(BigInteger, int, MathContext)): Likewise. + (multiply(BigDecimal, MathContext)): New method. + (negate(MathContext)): Likewise. + (plus(MathContext)): Likewise. + (numDigitsInLong): Fixed to properly handle negatives. + 2006-02-24 Anthony Balkissoon <abalkiss@redhat.com> * java/math/BigDecimal.java: diff --git a/java/math/BigDecimal.java b/java/math/BigDecimal.java index 2cdfdebac..91223d450 100644 --- a/java/math/BigDecimal.java +++ b/java/math/BigDecimal.java @@ -41,7 +41,6 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> { private BigInteger intVal; private int scale; - private MathContext mathContext; private int precision = 0; private static final long serialVersionUID = 6108874887143696463L; @@ -88,6 +87,27 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> } /** + * Constructs a BigDecimal using the BigDecimal(int) constructor and then + * rounds according to the MathContext. + * @param val the value for the initial (unrounded) BigDecimal + * @param mc the MathContext specifying the rounding + * @throws ArithmeticException if the result is inexact but the rounding type + * is RoundingMode.UNNECESSARY + * @since 1.5 + */ + public BigDecimal (int val, MathContext mc) + { + this (val); + if (mc.getPrecision() != 0) + { + BigDecimal result = this.round(mc); + this.intVal = result.intVal; + this.scale = result.scale; + this.precision = result.precision; + } + } + + /** * Constructs a new BigDecimal whose unscaled value is val and whose * scale is zero. * @param val the value of the new BigDecimal @@ -103,6 +123,8 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> * and then rounds according to the MathContext. * @param val the long from which we create the initial BigDecimal * @param mc the MathContext that specifies the rounding behaviour + * @throws ArithmeticException if the result is inexact but the rounding type + * is RoundingMode.UNNECESSARY * @since 1.5 */ public BigDecimal (long val, MathContext mc) @@ -124,7 +146,9 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> * the number of digits in num, then rounding is necessary. * @param num the unscaledValue, before rounding * @param mc the MathContext that specifies the precision - * @since 1.5 + * @throws ArithmeticException if the result is inexact but the rounding type + * is RoundingMode.UNNECESSARY + * * @since 1.5 */ public BigDecimal (BigInteger num, MathContext mc) { @@ -144,6 +168,8 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> * according to the MathContext mc. * @param val the String from which we construct the initial BigDecimal * @param mc the MathContext that specifies the rounding + * @throws ArithmeticException if the result is inexact but the rounding type + * is RoundingMode.UNNECESSARY * @since 1.5 */ public BigDecimal (String val, MathContext mc) @@ -179,12 +205,36 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> this.intVal = num; this.scale = scale; } + + /** + * Constructs a BigDecimal using the BigDecimal(BigInteger, int) + * constructor and then rounds according to the MathContext. + * @param num the unscaled value of the unrounded BigDecimal + * @param scale the scale of the unrounded BigDecimal + * @param mc the MathContext specifying the rounding + * @throws ArithmeticException if the result is inexact but the rounding type + * is RoundingMode.UNNECESSARY + * @since 1.5 + */ + public BigDecimal (BigInteger num, int scale, MathContext mc) + { + this (num, scale); + if (mc.getPrecision() != 0) + { + BigDecimal result = this.round(mc); + this.intVal = result.intVal; + this.scale = result.scale; + this.precision = result.precision; + } + } /** * Constructs a BigDecimal in the same way as BigDecimal(double) and then * rounds according to the MathContext. * @param num the double from which the initial BigDecimal is created * @param mc the MathContext that specifies the rounding behaviour + * @throws ArithmeticException if the result is inexact but the rounding type + * is RoundingMode.UNNECESSARY * @since 1.5 */ public BigDecimal (double num, MathContext mc) @@ -569,6 +619,21 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> { return new BigDecimal (intVal.multiply (val.intVal), scale + val.scale); } + + /** + * Returns a BigDecimal whose value is (this x val) before it is rounded + * according to the MathContext mc. + * @param val the multiplicand + * @param mc the MathContext for rounding + * @return a new BigDecimal with value approximately (this x val) + * @throws ArithmeticException if the value is inexact but the rounding mode + * is RoundingMode.UNNECESSARY + * @since 1.5 + */ + public BigDecimal multiply (BigDecimal val, MathContext mc) + { + return multiply(val).round(mc); + } public BigDecimal divide (BigDecimal val, int roundingMode) throws ArithmeticException, IllegalArgumentException @@ -763,6 +828,23 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> } /** + * Returns a BigDecimal whose value is found first by negating this via + * the negate() method, then by rounding according to the MathContext mc. + * @param mc the MathContext for rounding + * @return a BigDecimal whose value is approximately (-this) + * @throws ArithmeticException if the value is inexact but the rounding mode + * is RoundingMode.UNNECESSARY + * @since 1.5 + */ + public BigDecimal negate(MathContext mc) + { + BigDecimal result = negate(); + if (mc.getPrecision() != 0) + result = result.round(mc); + return result; + } + + /** * Returns this BigDecimal. This is included for symmetry with the * method negate(). * @return this @@ -774,6 +856,20 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> } /** + * Returns a BigDecimal whose value is found by rounding <code>this</code> + * according to the MathContext. This is the same as round(MathContext). + * @param mc the MathContext for rounding + * @return a BigDecimal whose value is <code>this</code> before being rounded + * @throws ArithmeticException if the value is inexact but the rounding mode + * is RoundingMode.UNNECESSARY + * @since 1.5 + */ + public BigDecimal plus(MathContext mc) + { + return round(mc); + } + + /** * Returns a BigDecimal which is this BigDecimal rounded according to the * MathContext rounding settings. * @param mc the MathContext that tells us how to round @@ -817,8 +913,9 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> * @param l the long value * @return the number of digits in l */ - private static int numDigitsInLong(long l) + private static int numDigitsInLong(long l1) { + long l = l1 >= 0 ? l1 : -l1; // We divide up the range in a binary fashion, this first if // takes care of numbers with 1 to 9 digits. if (l < 1000000000L) |