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 424c6c359b230554acf29afebf6c375e12b84d1d Author: aherbert <[email protected]> AuthorDate: Thu Apr 9 10:22:21 2020 +0100 Fraction: Add thrown exception to pow javadoc. Add test to the demonstrate exception due to overflow. --- .../src/main/java/org/apache/commons/numbers/fraction/Fraction.java | 1 + .../test/java/org/apache/commons/numbers/fraction/FractionTest.java | 5 +++++ 2 files changed, 6 insertions(+) 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 8f41989..fa7b377 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 @@ -642,6 +642,7 @@ public final class Fraction * * @param exponent exponent to which this {@code Fraction} is to be raised. * @return <code>this<sup>exponent</sup></code>. + * @throws ArithmeticException if the intermediate result would overflow. */ @Override public Fraction pow(final int exponent) { diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java index 082b884..2a5d5f1 100644 --- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java +++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java @@ -376,6 +376,11 @@ public class FractionTest { Fraction c = Fraction.of(0, -11); assertFraction(0, -1, c.pow(Integer.MAX_VALUE)); + + Assertions.assertThrows(ArithmeticException.class, () -> Fraction.of(Integer.MAX_VALUE).pow(2)); + Assertions.assertThrows(ArithmeticException.class, () -> Fraction.of(1, Integer.MAX_VALUE).pow(2)); + Assertions.assertThrows(ArithmeticException.class, () -> Fraction.of(Integer.MAX_VALUE).pow(-2)); + Assertions.assertThrows(ArithmeticException.class, () -> Fraction.of(1, Integer.MAX_VALUE).pow(-2)); } @Test
