You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2019/06/22 13:28:20 UTC
[commons-numbers] 01/04: NUMBERS-116: Remove addAndCheck methods
This is an automated email from the ASF dual-hosted git repository.
erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
commit 5c9d177a09f16d35d8db3b39b82c4bc577f853f3
Author: Schamschi <he...@gmx.at>
AuthorDate: Sat Jun 15 23:40:02 2019 +0200
NUMBERS-116: Remove addAndCheck methods
---
.../combinatorics/BinomialCoefficientTest.java | 2 +-
.../commons/numbers/core/ArithmeticUtils.java | 31 --------------
.../commons/numbers/core/ArithmeticUtilsTest.java | 47 ----------------------
.../apache/commons/numbers/fraction/Fraction.java | 2 +-
4 files changed, 2 insertions(+), 80 deletions(-)
diff --git a/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/BinomialCoefficientTest.java b/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/BinomialCoefficientTest.java
index 9c1dde2..71d26eb 100644
--- a/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/BinomialCoefficientTest.java
+++ b/commons-numbers-combinatorics/src/test/java/org/apache/commons/numbers/combinatorics/BinomialCoefficientTest.java
@@ -197,7 +197,7 @@ public class BinomialCoefficientTest {
if (k > 100) {
binomialCoefficient(n - 100, k - 100);
}
- result = ArithmeticUtils.addAndCheck(binomialCoefficient(n - 1, k - 1),
+ result = Math.addExact(binomialCoefficient(n - 1, k - 1),
binomialCoefficient(n - 1, k));
}
if (result == -1) {
diff --git a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/ArithmeticUtils.java b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/ArithmeticUtils.java
index 633dd3d..84f44aa 100644
--- a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/ArithmeticUtils.java
+++ b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/ArithmeticUtils.java
@@ -26,8 +26,6 @@ import java.text.MessageFormat;
*/
public final class ArithmeticUtils {
- /** Overflow addition exception message. */
- private static final String OVERFLOW_IN_ADDITION_MESSAGE = "overflow in addition: {0} + {1}";
/** Overflow gcd exception message for 2^31. */
private static final String OVERFLOW_GCD_MESSAGE_2_POWER_31 = "overflow: gcd({0}, {1}) is 2^31";
/** Overflow gcd exception message for 2^63. */
@@ -46,35 +44,6 @@ public final class ArithmeticUtils {
}
/**
- * Add two integers, checking for overflow.
- *
- * @param x an addend
- * @param y an addend
- * @return the sum {@code x+y}
- * @throws ArithmeticException if the result can not be represented
- * as an {@code int}.
- */
- public static int addAndCheck(int x, int y) {
- long s = (long)x + (long)y;
- if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) {
- throw new NumbersArithmeticException(OVERFLOW_IN_ADDITION_MESSAGE, x, y);
- }
- return (int)s;
- }
-
- /**
- * Add two long integers, checking for overflow.
- *
- * @param a an addend
- * @param b an addend
- * @return the sum {@code a+b}
- * @throws ArithmeticException if the result can not be represented as an long
- */
- public static long addAndCheck(long a, long b) {
- return addAndCheck(a, b, OVERFLOW_IN_ADDITION_MESSAGE);
- }
-
- /**
* Computes the greatest common divisor of the absolute value of two
* numbers, using a modified version of the "binary gcd" method.
* See Knuth 4.5.2 algorithm B.
diff --git a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java
index f23fe2d..edee964 100644
--- a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java
+++ b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/ArithmeticUtilsTest.java
@@ -30,44 +30,6 @@ import org.junit.jupiter.api.Test;
public class ArithmeticUtilsTest {
@Test
- public void testAddAndCheck() {
- int big = Integer.MAX_VALUE;
- int bigNeg = Integer.MIN_VALUE;
- Assertions.assertEquals(big, ArithmeticUtils.addAndCheck(big, 0));
- try {
- ArithmeticUtils.addAndCheck(big, 1);
- Assertions.fail("Expecting ArithmeticException");
- } catch (ArithmeticException ex) {
- }
- try {
- ArithmeticUtils.addAndCheck(bigNeg, -1);
- Assertions.fail("Expecting ArithmeticException");
- } catch (ArithmeticException ex) {
- }
- }
-
- @Test
- public void testAddAndCheckLong() {
- long max = Long.MAX_VALUE;
- long min = Long.MIN_VALUE;
- Assertions.assertEquals(max, ArithmeticUtils.addAndCheck(max, 0L));
- Assertions.assertEquals(min, ArithmeticUtils.addAndCheck(min, 0L));
- Assertions.assertEquals(max, ArithmeticUtils.addAndCheck(0L, max));
- Assertions.assertEquals(min, ArithmeticUtils.addAndCheck(0L, min));
- Assertions.assertEquals(1, ArithmeticUtils.addAndCheck(-1L, 2L));
- Assertions.assertEquals(1, ArithmeticUtils.addAndCheck(2L, -1L));
- Assertions.assertEquals(-3, ArithmeticUtils.addAndCheck(-2L, -1L));
- Assertions.assertEquals(min, ArithmeticUtils.addAndCheck(min + 1, -1L));
- Assertions.assertEquals(-1, ArithmeticUtils.addAndCheck(min, max));
- testAddAndCheckLongFailure(max, 1L);
- testAddAndCheckLongFailure(min, -1L);
- testAddAndCheckLongFailure(1L, max);
- testAddAndCheckLongFailure(-1L, min);
- testAddAndCheckLongFailure(max, max);
- testAddAndCheckLongFailure(min, min);
- }
-
- @Test
public void testGcd() {
int a = 30;
int b = 50;
@@ -563,15 +525,6 @@ public class ArithmeticUtilsTest {
}
}
- private void testAddAndCheckLongFailure(long a, long b) {
- try {
- ArithmeticUtils.addAndCheck(a, b);
- Assertions.fail("Expecting ArithmeticException");
- } catch (ArithmeticException ex) {
- // success
- }
- }
-
private void testMulAndCheckLongFailure(long a, long b) {
try {
ArithmeticUtils.mulAndCheck(a, b);
diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
index fad2d50..41132e0 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
@@ -474,7 +474,7 @@ public class Fraction
int d1 = ArithmeticUtils.gcd(denominator, fraction.denominator);
int uvp = ArithmeticUtils.mulAndCheck(numerator, fraction.denominator / d1);
int upv = ArithmeticUtils.mulAndCheck(fraction.numerator, denominator / d1);
- int t = isAdd ? ArithmeticUtils.addAndCheck(uvp, upv) : ArithmeticUtils.subAndCheck(uvp, upv);
+ int t = isAdd ? Math.addExact(uvp, upv) : ArithmeticUtils.subAndCheck(uvp, upv);
int tmodd1 = t % d1;
int d2 = (tmodd1==0)?d1:ArithmeticUtils.gcd(tmodd1, d1);
// result is (t/d2) / (u'/d1)(v'/d2)