From 3553ded10bd1518fd9cc023183e40a2cf44cc6f2 Mon Sep 17 00:00:00 2001 From: Sebastien Brisard Date: Mon, 27 Feb 2012 04:38:45 +0000 Subject: [PATCH] BigFraction.divide(BigFraction) now throws MathArithmeticException (see MATH-755). git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1294027 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/math3/fraction/BigFraction.java | 51 +++++++++---------- .../math3/fraction/BigFractionTest.java | 5 +- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/apache/commons/math3/fraction/BigFraction.java b/src/main/java/org/apache/commons/math3/fraction/BigFraction.java index 5a44b4f42..5b0a6b4ce 100644 --- a/src/main/java/org/apache/commons/math3/fraction/BigFraction.java +++ b/src/main/java/org/apache/commons/math3/fraction/BigFraction.java @@ -21,13 +21,14 @@ import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.math3.FieldElement; +import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.util.LocalizedFormats; +import org.apache.commons.math3.util.ArithmeticUtils; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; -import org.apache.commons.math3.util.ArithmeticUtils; /** * Representation of a rational number without any overflow. This class is @@ -598,36 +599,34 @@ public class BigFraction /** *

- * Divide the value of this fraction by the passed BigInteger, - * ie "this * 1 / bg", returning the result in reduced form. + * Divide the value of this fraction by the passed {@code BigInteger}, + * ie {@code this * 1 / bg}, returning the result in reduced form. *

* - * @param bg - * the BigInteger to divide by, must not be - * null. - * @return a {@link BigFraction} instance with the resulting values. - * @throws NullArgumentException if the {@code BigInteger} is {@code null}. - * @throws ZeroException - * if the fraction to divide by is zero. + * @param bg the {@code BigInteger} to divide by, must not be {@code null} + * @return a {@link BigFraction} instance with the resulting values + * @throws NullArgumentException if the {@code BigInteger} is {@code null} + * @throws MathArithmeticException if the fraction to divide by is zero */ public BigFraction divide(final BigInteger bg) { + if (bg == null) { + throw new NullArgumentException(LocalizedFormats.FRACTION); + } if (BigInteger.ZERO.equals(bg)) { - throw new ZeroException(LocalizedFormats.ZERO_DENOMINATOR); + throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR); } return new BigFraction(numerator, denominator.multiply(bg)); } /** *

- * Divide the value of this fraction by the passed int, ie - * "this * 1 / i", returning the result in reduced form. + * Divide the value of this fraction by the passed {@code int}, ie + * {@code this * 1 / i}, returning the result in reduced form. *

* - * @param i - * the int to divide by. - * @return a {@link BigFraction} instance with the resulting values. - * @throws ArithmeticException - * if the fraction to divide by is zero. + * @param i the {@code int} to divide by + * @return a {@link BigFraction} instance with the resulting values + * @throws MathArithmeticException if the fraction to divide by is zero */ public BigFraction divide(final int i) { return divide(BigInteger.valueOf(i)); @@ -635,15 +634,13 @@ public class BigFraction /** *

- * Divide the value of this fraction by the passed long, ie - * "this * 1 / l", returning the result in reduced form. + * Divide the value of this fraction by the passed {@code long}, ie + * {@code this * 1 / l}, returning the result in reduced form. *

* - * @param l - * the long to divide by. - * @return a {@link BigFraction} instance with the resulting values. - * @throws ArithmeticException - * if the fraction to divide by is zero. + * @param l the {@code long} to divide by + * @return a {@link BigFraction} instance with the resulting values + * @throws MathArithmeticException if the fraction to divide by is zero */ public BigFraction divide(final long l) { return divide(BigInteger.valueOf(l)); @@ -658,14 +655,14 @@ public class BigFraction * @param fraction Fraction to divide by, must not be {@code null}. * @return a {@link BigFraction} instance with the resulting values. * @throws NullArgumentException if the {@code fraction} is {@code null}. - * @throws ZeroException if the fraction to divide by is zero. + * @throws MathArithmeticException if the fraction to divide by is zero */ public BigFraction divide(final BigFraction fraction) { if (fraction == null) { throw new NullArgumentException(LocalizedFormats.FRACTION); } if (BigInteger.ZERO.equals(fraction.numerator)) { - throw new ZeroException(LocalizedFormats.ZERO_DENOMINATOR); + throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR); } return multiply(fraction.reciprocal()); diff --git a/src/test/java/org/apache/commons/math3/fraction/BigFractionTest.java b/src/test/java/org/apache/commons/math3/fraction/BigFractionTest.java index 6e10f791f..1181d1686 100644 --- a/src/test/java/org/apache/commons/math3/fraction/BigFractionTest.java +++ b/src/test/java/org/apache/commons/math3/fraction/BigFractionTest.java @@ -21,6 +21,7 @@ import java.math.BigInteger; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.exception.ConvergenceException; +import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.util.FastMath; @@ -423,8 +424,8 @@ public class BigFractionTest { BigFraction f2 = BigFraction.ZERO; try { f1.divide(f2); - Assert.fail("expecting ArithmeticException"); - } catch (ZeroException ex) { + Assert.fail("expecting MathArithmeticException"); + } catch (MathArithmeticException ex) { } f1 = new BigFraction(0, 5);