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
This commit is contained in:
parent
4705c4f924
commit
3553ded10b
|
@ -21,13 +21,14 @@ import java.math.BigDecimal;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
|
|
||||||
import org.apache.commons.math3.FieldElement;
|
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.MathIllegalArgumentException;
|
||||||
import org.apache.commons.math3.exception.NullArgumentException;
|
import org.apache.commons.math3.exception.NullArgumentException;
|
||||||
import org.apache.commons.math3.exception.ZeroException;
|
import org.apache.commons.math3.exception.ZeroException;
|
||||||
import org.apache.commons.math3.exception.util.LocalizedFormats;
|
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.FastMath;
|
||||||
import org.apache.commons.math3.util.MathUtils;
|
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
|
* Representation of a rational number without any overflow. This class is
|
||||||
|
@ -598,36 +599,34 @@ public class BigFraction
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Divide the value of this fraction by the passed <code>BigInteger</code>,
|
* Divide the value of this fraction by the passed {@code BigInteger},
|
||||||
* ie "this * 1 / bg", returning the result in reduced form.
|
* ie {@code this * 1 / bg}, returning the result in reduced form.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param bg
|
* @param bg the {@code BigInteger} to divide by, must not be {@code null}
|
||||||
* the <code>BigInteger</code> to divide by, must not be
|
* @return a {@link BigFraction} instance with the resulting values
|
||||||
* <code>null</code>.
|
* @throws NullArgumentException if the {@code BigInteger} is {@code null}
|
||||||
* @return a {@link BigFraction} instance with the resulting values.
|
* @throws MathArithmeticException if the fraction to divide by is zero
|
||||||
* @throws NullArgumentException if the {@code BigInteger} is {@code null}.
|
|
||||||
* @throws ZeroException
|
|
||||||
* if the fraction to divide by is zero.
|
|
||||||
*/
|
*/
|
||||||
public BigFraction divide(final BigInteger bg) {
|
public BigFraction divide(final BigInteger bg) {
|
||||||
|
if (bg == null) {
|
||||||
|
throw new NullArgumentException(LocalizedFormats.FRACTION);
|
||||||
|
}
|
||||||
if (BigInteger.ZERO.equals(bg)) {
|
if (BigInteger.ZERO.equals(bg)) {
|
||||||
throw new ZeroException(LocalizedFormats.ZERO_DENOMINATOR);
|
throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
|
||||||
}
|
}
|
||||||
return new BigFraction(numerator, denominator.multiply(bg));
|
return new BigFraction(numerator, denominator.multiply(bg));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Divide the value of this fraction by the passed <tt>int</tt>, ie
|
* Divide the value of this fraction by the passed {@code int}, ie
|
||||||
* "this * 1 / i", returning the result in reduced form.
|
* {@code this * 1 / i}, returning the result in reduced form.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param i
|
* @param i the {@code int} to divide by
|
||||||
* the <tt>int</tt> to divide by.
|
* @return a {@link BigFraction} instance with the resulting values
|
||||||
* @return a {@link BigFraction} instance with the resulting values.
|
* @throws MathArithmeticException if the fraction to divide by is zero
|
||||||
* @throws ArithmeticException
|
|
||||||
* if the fraction to divide by is zero.
|
|
||||||
*/
|
*/
|
||||||
public BigFraction divide(final int i) {
|
public BigFraction divide(final int i) {
|
||||||
return divide(BigInteger.valueOf(i));
|
return divide(BigInteger.valueOf(i));
|
||||||
|
@ -635,15 +634,13 @@ public class BigFraction
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Divide the value of this fraction by the passed <tt>long</tt>, ie
|
* Divide the value of this fraction by the passed {@code long}, ie
|
||||||
* "this * 1 / l", returning the result in reduced form.
|
* {@code this * 1 / l}, returning the result in reduced form.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param l
|
* @param l the {@code long} to divide by
|
||||||
* the <tt>long</tt> to divide by.
|
* @return a {@link BigFraction} instance with the resulting values
|
||||||
* @return a {@link BigFraction} instance with the resulting values.
|
* @throws MathArithmeticException if the fraction to divide by is zero
|
||||||
* @throws ArithmeticException
|
|
||||||
* if the fraction to divide by is zero.
|
|
||||||
*/
|
*/
|
||||||
public BigFraction divide(final long l) {
|
public BigFraction divide(final long l) {
|
||||||
return divide(BigInteger.valueOf(l));
|
return divide(BigInteger.valueOf(l));
|
||||||
|
@ -658,14 +655,14 @@ public class BigFraction
|
||||||
* @param fraction Fraction to divide by, must not be {@code null}.
|
* @param fraction Fraction to divide by, must not be {@code null}.
|
||||||
* @return a {@link BigFraction} instance with the resulting values.
|
* @return a {@link BigFraction} instance with the resulting values.
|
||||||
* @throws NullArgumentException if the {@code fraction} is {@code null}.
|
* @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) {
|
public BigFraction divide(final BigFraction fraction) {
|
||||||
if (fraction == null) {
|
if (fraction == null) {
|
||||||
throw new NullArgumentException(LocalizedFormats.FRACTION);
|
throw new NullArgumentException(LocalizedFormats.FRACTION);
|
||||||
}
|
}
|
||||||
if (BigInteger.ZERO.equals(fraction.numerator)) {
|
if (BigInteger.ZERO.equals(fraction.numerator)) {
|
||||||
throw new ZeroException(LocalizedFormats.ZERO_DENOMINATOR);
|
throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
|
||||||
}
|
}
|
||||||
|
|
||||||
return multiply(fraction.reciprocal());
|
return multiply(fraction.reciprocal());
|
||||||
|
|
|
@ -21,6 +21,7 @@ import java.math.BigInteger;
|
||||||
|
|
||||||
import org.apache.commons.math3.TestUtils;
|
import org.apache.commons.math3.TestUtils;
|
||||||
import org.apache.commons.math3.exception.ConvergenceException;
|
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.NullArgumentException;
|
||||||
import org.apache.commons.math3.exception.ZeroException;
|
import org.apache.commons.math3.exception.ZeroException;
|
||||||
import org.apache.commons.math3.util.FastMath;
|
import org.apache.commons.math3.util.FastMath;
|
||||||
|
@ -423,8 +424,8 @@ public class BigFractionTest {
|
||||||
BigFraction f2 = BigFraction.ZERO;
|
BigFraction f2 = BigFraction.ZERO;
|
||||||
try {
|
try {
|
||||||
f1.divide(f2);
|
f1.divide(f2);
|
||||||
Assert.fail("expecting ArithmeticException");
|
Assert.fail("expecting MathArithmeticException");
|
||||||
} catch (ZeroException ex) {
|
} catch (MathArithmeticException ex) {
|
||||||
}
|
}
|
||||||
|
|
||||||
f1 = new BigFraction(0, 5);
|
f1 = new BigFraction(0, 5);
|
||||||
|
|
Loading…
Reference in New Issue