Changed MathUtils.round(double,int,int) to propagate rather than

wrap runtime exceptions.  Instead of MathRuntimeException, this method
now throws IllegalArgumentException or ArithmeticException under
the conditions specified in the javadoc.
JIRA: MATH-555




git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1088473 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2011-04-04 04:51:37 +00:00
parent fbbb96eb17
commit 328513f3ad
3 changed files with 19 additions and 9 deletions

View File

@ -33,7 +33,6 @@ import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.NotPositiveException; import org.apache.commons.math.exception.NotPositiveException;
import org.apache.commons.math.exception.MathArithmeticException; import org.apache.commons.math.exception.MathArithmeticException;
import org.apache.commons.math.exception.MathIllegalArgumentException; import org.apache.commons.math.exception.MathIllegalArgumentException;
import org.apache.commons.math.exception.MathRuntimeException;
import org.apache.commons.math.exception.NumberIsTooLargeException; import org.apache.commons.math.exception.NumberIsTooLargeException;
import org.apache.commons.math.exception.NotFiniteNumberException; import org.apache.commons.math.exception.NotFiniteNumberException;
@ -1333,15 +1332,22 @@ public final class MathUtils {
} }
/** /**
* Round the given value to the specified number of decimal places. The * <p>Round the given value to the specified number of decimal places. The
* value is rounded using the given method which is any method defined in * value is rounded using the given method which is any method defined in
* {@link BigDecimal}. * {@link BigDecimal}.</p>
*
* <p>If {@code x} is infinite or NaN, then the value of {@code x} is
* returned unchanged, regardless of the other parameters.</p>
* *
* @param x the value to round. * @param x the value to round.
* @param scale the number of digits to the right of the decimal point. * @param scale the number of digits to the right of the decimal point.
* @param roundingMethod the rounding method as defined in * @param roundingMethod the rounding method as defined in
* {@link BigDecimal}. * {@link BigDecimal}.
* @return the rounded value. * @return the rounded value.
* @throws ArithmeticException if roundingMethod==ROUND_UNNECESSARY and the
* specified scaling operation would require rounding.
* @throws IllegalArgumentException if roundingMethod does not represent a
* valid rounding mode.
* @since 1.1 * @since 1.1
*/ */
public static double round(double x, int scale, int roundingMethod) { public static double round(double x, int scale, int roundingMethod) {
@ -1356,8 +1362,6 @@ public final class MathUtils {
} else { } else {
return Double.NaN; return Double.NaN;
} }
} catch (RuntimeException ex) {
throw new MathRuntimeException(ex);
} }
} }

View File

@ -52,6 +52,12 @@ The <action> type attribute can be add,update,fix,remove.
If the output is not quite correct, check for invisible trailing spaces! If the output is not quite correct, check for invisible trailing spaces!
--> -->
<release version="3.0" date="TBD" description="TBD"> <release version="3.0" date="TBD" description="TBD">
<action dev="psteitz" type="update" issue="MATH-555">
Changed MathUtils.round(double,int,int) to propagate rather than
wrap runtime exceptions. Instead of MathRuntimeException, this method
now throws IllegalArgumentException or ArithmeticException under
the conditions specified in the javadoc.
</action>
<action dev="luc" type="fix" issue="MATH-554" > <action dev="luc" type="fix" issue="MATH-554" >
Reduced cancellation errors in Vector3D.crossProduct Reduced cancellation errors in Vector3D.crossProduct
</action> </action>

View File

@ -1136,8 +1136,8 @@ public final class MathUtilsTest {
try { try {
MathUtils.round(1.234, 2, BigDecimal.ROUND_UNNECESSARY); MathUtils.round(1.234, 2, BigDecimal.ROUND_UNNECESSARY);
Assert.fail(); Assert.fail();
} catch (MathRuntimeException ex) { // XXX Loosing semantics? } catch (ArithmeticException ex) {
// success // expected
} }
Assert.assertEquals(1.24, MathUtils.round(x, 2, BigDecimal.ROUND_UP), 0.0); Assert.assertEquals(1.24, MathUtils.round(x, 2, BigDecimal.ROUND_UP), 0.0);
@ -1150,8 +1150,8 @@ public final class MathUtilsTest {
try { try {
MathUtils.round(1.234, 2, 1923); MathUtils.round(1.234, 2, 1923);
Assert.fail(); Assert.fail();
} catch (MathRuntimeException ex) { // XXX Loosing semantics? } catch (IllegalArgumentException ex) {
// success // expected
} }
// MATH-151 // MATH-151