Skip to content

Commit 39c4767

Browse files
author
Gilles Sadowski
committed
MATH-1629: Throw "ArithmeticException" instead of "MathArithmeticException".
1 parent cb5f001 commit 39c4767

3 files changed

Lines changed: 78 additions & 63 deletions

File tree

commons-math-legacy-core/src/main/java/org/apache/commons/math4/legacy/core/jdkmath/AccurateMath.java

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import java.io.PrintStream;
2020

2121
import org.apache.commons.numbers.core.Precision;
22-
import org.apache.commons.math4.legacy.exception.MathArithmeticException;
23-
import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
2422

2523
/**
2624
* Portable alternative to {@link Math} and {@link StrictMath}.
@@ -86,6 +84,10 @@ public final class AccurateMath {
8684
/** Exponential fractions table length. */
8785
static final int EXP_FRAC_TABLE_LEN = 1025; // 0, 1/1024, ... 1024/1024
8886

87+
/** Error message. */
88+
private static final String ZERO_DENOMINATOR_MSG = "Division by zero";
89+
/** Error message. */
90+
private static final String OVERFLOW_MSG = "Overflow";
8991
/** StrictMath.log(Double.MAX_VALUE): {@value}. */
9092
private static final double LOG_MAX_VALUE = StrictMath.log(Double.MAX_VALUE);
9193

@@ -3729,64 +3731,65 @@ public static double IEEEremainder(final double dividend, final double divisor)
37293731
/** Convert a long to integer, detecting overflows.
37303732
* @param n number to convert to int
37313733
* @return integer with same value as n if no overflows occur
3732-
* @exception MathArithmeticException if n cannot fit into an int
3734+
* @exception ArithmeticException if n cannot fit into an int
37333735
* @since 3.4
37343736
*/
37353737
public static int toIntExact(final long n) {
3736-
if (n < Integer.MIN_VALUE || n > Integer.MAX_VALUE) {
3737-
throw new MathArithmeticException(LocalizedFormats.OVERFLOW);
3738+
if (n < Integer.MIN_VALUE ||
3739+
n > Integer.MAX_VALUE) {
3740+
throw new ArithmeticException(OVERFLOW_MSG);
37383741
}
37393742
return (int) n;
37403743
}
37413744

37423745
/** Increment a number, detecting overflows.
37433746
* @param n number to increment
37443747
* @return n+1 if no overflows occur
3745-
* @exception MathArithmeticException if an overflow occurs
3748+
* @exception ArithmeticException if an overflow occurs
37463749
* @since 3.4
37473750
*/
37483751
public static int incrementExact(final int n) {
37493752
if (n == Integer.MAX_VALUE) {
3750-
throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, n, 1);
3753+
throw new ArithmeticException(OVERFLOW_MSG);
37513754
}
37523755
return n + 1;
37533756
}
37543757

37553758
/** Increment a number, detecting overflows.
37563759
* @param n number to increment
37573760
* @return n+1 if no overflows occur
3758-
* @exception MathArithmeticException if an overflow occurs
3761+
* @exception ArithmeticException if an overflow occurs
37593762
* @since 3.4
37603763
*/
37613764
public static long incrementExact(final long n) {
37623765
if (n == Long.MAX_VALUE) {
3763-
throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, n, 1);
3766+
throw new ArithmeticException(OVERFLOW_MSG);
37643767
}
37653768
return n + 1;
37663769
}
37673770

37683771
/** Decrement a number, detecting overflows.
37693772
* @param n number to decrement
37703773
* @return n-1 if no overflows occur
3771-
* @exception MathArithmeticException if an overflow occurs
3774+
* @exception ArithmeticException if an overflow occurs
37723775
* @since 3.4
37733776
*/
37743777
public static int decrementExact(final int n) {
37753778
if (n == Integer.MIN_VALUE) {
3776-
throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_SUBTRACTION, n, 1);
3779+
throw new ArithmeticException(OVERFLOW_MSG);
37773780
}
37783781
return n - 1;
37793782
}
37803783

37813784
/** Decrement a number, detecting overflows.
37823785
* @param n number to decrement
37833786
* @return n-1 if no overflows occur
3784-
* @exception MathArithmeticException if an overflow occurs
3787+
* @exception ArithmeticException if an overflow occurs
37853788
* @since 3.4
37863789
*/
37873790
public static long decrementExact(final long n) {
37883791
if (n == Long.MIN_VALUE) {
3789-
throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_SUBTRACTION, n, 1);
3792+
throw new ArithmeticException(OVERFLOW_MSG);
37903793
}
37913794
return n - 1;
37923795
}
@@ -3795,16 +3798,17 @@ public static long decrementExact(final long n) {
37953798
* @param a first number to add
37963799
* @param b second number to add
37973800
* @return a+b if no overflows occur
3798-
* @exception MathArithmeticException if an overflow occurs
3801+
* @exception ArithmeticException if an overflow occurs
37993802
* @since 3.4
38003803
*/
38013804
public static int addExact(final int a, final int b) {
38023805
// compute sum
38033806
final int sum = a + b;
38043807

38053808
// check for overflow
3806-
if ((a ^ b) >= 0 && (sum ^ b) < 0) {
3807-
throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, a, b);
3809+
if ((a ^ b) >= 0 &&
3810+
(sum ^ b) < 0) {
3811+
throw new ArithmeticException(OVERFLOW_MSG);
38083812
}
38093813

38103814
return sum;
@@ -3814,16 +3818,17 @@ public static int addExact(final int a, final int b) {
38143818
* @param a first number to add
38153819
* @param b second number to add
38163820
* @return a+b if no overflows occur
3817-
* @exception MathArithmeticException if an overflow occurs
3821+
* @exception ArithmeticException if an overflow occurs
38183822
* @since 3.4
38193823
*/
38203824
public static long addExact(final long a, final long b) {
38213825
// compute sum
38223826
final long sum = a + b;
38233827

38243828
// check for overflow
3825-
if ((a ^ b) >= 0 && (sum ^ b) < 0) {
3826-
throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, a, b);
3829+
if ((a ^ b) >= 0 &&
3830+
(sum ^ b) < 0) {
3831+
throw new ArithmeticException(OVERFLOW_MSG);
38273832
}
38283833

38293834
return sum;
@@ -3833,16 +3838,17 @@ public static long addExact(final long a, final long b) {
38333838
* @param a first number
38343839
* @param b second number to subtract from a
38353840
* @return a-b if no overflows occur
3836-
* @exception MathArithmeticException if an overflow occurs
3841+
* @exception ArithmeticException if an overflow occurs
38373842
* @since 3.4
38383843
*/
38393844
public static int subtractExact(final int a, final int b) {
38403845
// compute subtraction
38413846
final int sub = a - b;
38423847

38433848
// check for overflow
3844-
if ((a ^ b) < 0 && (sub ^ b) >= 0) {
3845-
throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_SUBTRACTION, a, b);
3849+
if ((a ^ b) < 0 &&
3850+
(sub ^ b) >= 0) {
3851+
throw new ArithmeticException(OVERFLOW_MSG);
38463852
}
38473853

38483854
return sub;
@@ -3852,16 +3858,17 @@ public static int subtractExact(final int a, final int b) {
38523858
* @param a first number
38533859
* @param b second number to subtract from a
38543860
* @return a-b if no overflows occur
3855-
* @exception MathArithmeticException if an overflow occurs
3861+
* @exception ArithmeticException if an overflow occurs
38563862
* @since 3.4
38573863
*/
38583864
public static long subtractExact(final long a, final long b) {
38593865
// compute subtraction
38603866
final long sub = a - b;
38613867

38623868
// check for overflow
3863-
if ((a ^ b) < 0 && (sub ^ b) >= 0) {
3864-
throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_SUBTRACTION, a, b);
3869+
if ((a ^ b) < 0 &&
3870+
(sub ^ b) >= 0) {
3871+
throw new ArithmeticException(OVERFLOW_MSG);
38653872
}
38663873

38673874
return sub;
@@ -3871,14 +3878,19 @@ public static long subtractExact(final long a, final long b) {
38713878
* @param a first number to multiply
38723879
* @param b second number to multiply
38733880
* @return a*b if no overflows occur
3874-
* @exception MathArithmeticException if an overflow occurs
3881+
* @exception ArithmeticException if an overflow occurs
38753882
* @since 3.4
38763883
*/
38773884
public static int multiplyExact(final int a, final int b) {
3878-
if (((b > 0) && (a > Integer.MAX_VALUE / b || a < Integer.MIN_VALUE / b)) ||
3879-
((b < -1) && (a > Integer.MIN_VALUE / b || a < Integer.MAX_VALUE / b)) ||
3880-
((b == -1) && (a == Integer.MIN_VALUE))) {
3881-
throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_MULTIPLICATION, a, b);
3885+
if (((b > 0) &&
3886+
(a > Integer.MAX_VALUE / b ||
3887+
a < Integer.MIN_VALUE / b)) ||
3888+
((b < -1) &&
3889+
(a > Integer.MIN_VALUE / b ||
3890+
a < Integer.MAX_VALUE / b)) ||
3891+
((b == -1) &&
3892+
(a == Integer.MIN_VALUE))) {
3893+
throw new ArithmeticException(OVERFLOW_MSG);
38823894
}
38833895
return a * b;
38843896
}
@@ -3887,14 +3899,19 @@ public static int multiplyExact(final int a, final int b) {
38873899
* @param a first number to multiply
38883900
* @param b second number to multiply
38893901
* @return a*b if no overflows occur
3890-
* @exception MathArithmeticException if an overflow occurs
3902+
* @exception ArithmeticException if an overflow occurs
38913903
* @since 3.4
38923904
*/
38933905
public static long multiplyExact(final long a, final long b) {
3894-
if (((b > 0L) && (a > Long.MAX_VALUE / b || a < Long.MIN_VALUE / b)) ||
3895-
((b < -1L) && (a > Long.MIN_VALUE / b || a < Long.MAX_VALUE / b)) ||
3896-
((b == -1L) && (a == Long.MIN_VALUE))) {
3897-
throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_MULTIPLICATION, a, b);
3906+
if (((b > 0L) &&
3907+
(a > Long.MAX_VALUE / b ||
3908+
a < Long.MIN_VALUE / b)) ||
3909+
((b < -1L) &&
3910+
(a > Long.MIN_VALUE / b ||
3911+
a < Long.MAX_VALUE / b)) ||
3912+
((b == -1L) &&
3913+
(a == Long.MIN_VALUE))) {
3914+
throw new ArithmeticException(OVERFLOW_MSG);
38983915
}
38993916
return a * b;
39003917
}
@@ -3908,13 +3925,13 @@ public static long multiplyExact(final long a, final long b) {
39083925
* @param a dividend
39093926
* @param b divisor
39103927
* @return q such that a = q b + r with 0 &lt;= r &lt; b if b &gt; 0 and b &lt; r &lt;= 0 if b &lt; 0
3911-
* @exception MathArithmeticException if b == 0
3928+
* @exception ArithmeticException if b == 0
39123929
* @see #floorMod(int, int)
39133930
* @since 3.4
39143931
*/
39153932
public static int floorDiv(final int a, final int b) {
39163933
if (b == 0) {
3917-
throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
3934+
throw new ArithmeticException(ZERO_DENOMINATOR_MSG);
39183935
}
39193936

39203937
final int m = a % b;
@@ -3936,13 +3953,13 @@ public static int floorDiv(final int a, final int b) {
39363953
* @param a dividend
39373954
* @param b divisor
39383955
* @return q such that a = q b + r with 0 &lt;= r &lt; b if b &gt; 0 and b &lt; r &lt;= 0 if b &lt; 0
3939-
* @exception MathArithmeticException if b == 0
3956+
* @exception ArithmeticException if b == 0
39403957
* @see #floorMod(long, long)
39413958
* @since 3.4
39423959
*/
39433960
public static long floorDiv(final long a, final long b) {
39443961
if (b == 0L) {
3945-
throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
3962+
throw new ArithmeticException(ZERO_DENOMINATOR_MSG);
39463963
}
39473964

39483965
final long m = a % b;
@@ -3964,13 +3981,13 @@ public static long floorDiv(final long a, final long b) {
39643981
* @param a dividend
39653982
* @param b divisor
39663983
* @return r such that a = q b + r with 0 &lt;= r &lt; b if b &gt; 0 and b &lt; r &lt;= 0 if b &lt; 0
3967-
* @exception MathArithmeticException if b == 0
3984+
* @exception ArithmeticException if b == 0
39683985
* @see #floorDiv(int, int)
39693986
* @since 3.4
39703987
*/
39713988
public static int floorMod(final int a, final int b) {
39723989
if (b == 0) {
3973-
throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
3990+
throw new ArithmeticException(ZERO_DENOMINATOR_MSG);
39743991
}
39753992

39763993
final int m = a % b;
@@ -3992,13 +4009,13 @@ public static int floorMod(final int a, final int b) {
39924009
* @param a dividend
39934010
* @param b divisor
39944011
* @return r such that a = q b + r with 0 &lt;= r &lt; b if b &gt; 0 and b &lt; r &lt;= 0 if b &lt; 0
3995-
* @exception MathArithmeticException if b == 0
4012+
* @exception ArithmeticException if b == 0
39964013
* @see #floorDiv(long, long)
39974014
* @since 3.4
39984015
*/
39994016
public static long floorMod(final long a, final long b) {
40004017
if (b == 0L) {
4001-
throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR);
4018+
throw new ArithmeticException(ZERO_DENOMINATOR_MSG);
40024019
}
40034020

40044021
final long m = a % b;

commons-math-legacy-core/src/test/java/org/apache/commons/math4/legacy/core/jdkmath/AccurateMathStrictComparisonTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Arrays;
2525
import java.util.List;
2626

27-
import org.apache.commons.math4.legacy.exception.MathArithmeticException;
2827
import org.apache.commons.numbers.core.Precision;
2928
import org.junit.Assert;
3029
import org.junit.Test;
@@ -179,8 +178,8 @@ private static void callMethods(Method mathMethod, Method fastMethod,
179178
actual = ite.getCause();
180179
}
181180
if (expected instanceof ArithmeticException) {
182-
Assert.assertEquals(MathArithmeticException.class, actual.getClass());
183-
} else if (!expected.equals(actual)) {
181+
Assert.assertTrue(actual instanceof ArithmeticException);
182+
} else if (!expected.equals(actual)) {
184183
reportFailedResults(mathMethod, params, expected, actual, entries);
185184
}
186185
} catch (IllegalArgumentException e) {

0 commit comments

Comments
 (0)