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
The following commit(s) were added to refs/heads/master by this push:
new 8cfc04e8 NUMBERS-218: Fix representable minimum-value powers
8cfc04e8 is described below
commit 8cfc04e8719d00bde40b3faba19180cc92e20f9e
Author: AryanRG <[email protected]>
AuthorDate: Sun Oct 18 03:51:48 2026 +0530
NUMBERS-218: Fix representable minimum-value powers
---
.../org/apache/commons/numbers/core/ArithmeticUtils.java | 4 ++--
.../apache/commons/numbers/core/ArithmeticUtilsTest.java | 14 ++++++++++++++
src/changes/changes.xml | 4 ++++
3 files changed, 20 insertions(+), 2 deletions(-)
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 153b3202..b5d4ad70 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
@@ -294,7 +294,7 @@ public final class ArithmeticUtils {
return (e & 1) == 0 ? 1 : -1;
}
- if (e >= 31) {
+ if (e > 31) {
throw new ArithmeticException("integer overflow");
}
@@ -354,7 +354,7 @@ public final class ArithmeticUtils {
return (e & 1) == 0 ? 1L : -1L;
}
- if (e >= 63) {
+ if (e > 63) {
throw new ArithmeticException("long overflow");
}
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 b77ae08f..057471d5 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
@@ -423,6 +423,20 @@ class ArithmeticUtilsTest {
}
}
+ @Test
+ void testPowMinValue() {
+ // (-2)^31 and (-2)^63 are exactly representable as the minimum value
+ Assertions.assertEquals(Integer.MIN_VALUE, ArithmeticUtils.pow(-2,
31));
+ Assertions.assertEquals(Long.MIN_VALUE, ArithmeticUtils.pow(-2L, 63));
+ // Adjacent results overflow
+ Assertions.assertThrows(ArithmeticException.class, () ->
ArithmeticUtils.pow(2, 31));
+ Assertions.assertThrows(ArithmeticException.class, () ->
ArithmeticUtils.pow(-2, 32));
+ Assertions.assertThrows(ArithmeticException.class, () ->
ArithmeticUtils.pow(3, 31));
+ Assertions.assertThrows(ArithmeticException.class, () ->
ArithmeticUtils.pow(2L, 63));
+ Assertions.assertThrows(ArithmeticException.class, () ->
ArithmeticUtils.pow(-2L, 64));
+ Assertions.assertThrows(ArithmeticException.class, () ->
ArithmeticUtils.pow(3L, 63));
+ }
+
@Test
void testPowEdgeCases() {
Assertions.assertEquals(0, ArithmeticUtils.pow(0, 2));
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index f30c55f8..704ab630 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -56,6 +56,10 @@ If the output is not quite correct, check for invisible
trailing spaces!
<release version="1.4" date="TBD" description="
New features, updates and bug fixes.
">
+ <action type="fix" due-to="Aryan Ganjoo" issue="NUMBERS-218">
+ "ArithmeticUtils.pow": Return the representable minimum int and long
values for
+ (-2)^31 and (-2)^63 instead of reporting overflow.
+ </action>
<action dev="aherbert" type="update" due-to="Security scan, Alex
Herbert" issue="NUMBERS-214">
"GeneralizedContinuedFraction": Lower the defaut iterations to
1_000_000. This
introduces a behavioural change for slowly converging fractions that
may now