You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2020/04/09 11:34:46 UTC
[commons-numbers] 04/07: Document and test for overflow in
BigFraction.pow(int)
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
commit 99c627e57e5bb14ada31f33809aa62356bfbb7bd
Author: aherbert <ah...@apache.org>
AuthorDate: Thu Apr 9 12:22:13 2020 +0100
Document and test for overflow in BigFraction.pow(int)
---
.../main/java/org/apache/commons/numbers/fraction/BigFraction.java | 1 +
.../java/org/apache/commons/numbers/fraction/BigFractionTest.java | 7 +++++++
2 files changed, 8 insertions(+)
diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
index ded343b..0d145fb 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
@@ -888,6 +888,7 @@ public final class BigFraction
*
* @param exponent exponent to which this {@code BigFraction} is to be raised.
* @return <code>this<sup>exponent</sup></code>.
+ * @throws ArithmeticException if the intermediate result would overflow.
*/
@Override
public BigFraction pow(final int exponent) {
diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
index 1f0f52e..40dc9e9 100644
--- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
+++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
@@ -641,6 +641,13 @@ public class BigFractionTest {
assertFraction(testCase.expectedNumerator, testCase.expectedDenominator, f1.pow(exponent));
}
+ // Note: BigInteger magnitude is limited to 2^Integer.MAX_VALUE exclusive
+ // in the reference implementation (up to at least JDK 14).
+ Assertions.assertThrows(ArithmeticException.class, () -> BigFraction.of(2).pow(Integer.MAX_VALUE));
+ Assertions.assertThrows(ArithmeticException.class, () -> BigFraction.of(1, 2).pow(Integer.MAX_VALUE));
+ Assertions.assertThrows(ArithmeticException.class, () -> BigFraction.of(2).pow(-Integer.MAX_VALUE));
+ Assertions.assertThrows(ArithmeticException.class, () -> BigFraction.of(1, 2).pow(-Integer.MAX_VALUE));
+
Assertions.assertEquals(BigFraction.of(8192, 1594323), BigFraction.of(2, 3).pow(13));
Assertions.assertEquals(BigFraction.of(8192, 1594323), BigFraction.of(2, 3).pow(13L));
Assertions.assertEquals(BigFraction.of(8192, 1594323), BigFraction.of(2, 3).pow(BigInteger.valueOf(13L)));