Repository: commons-math Updated Branches: refs/heads/master 941b13f8e -> d93c95d53
Fixed infinite loop in FastMath.pow(double, long) with Long.MIN_VALUE. JIRA: MATH-1272 Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/26e878ab Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/26e878ab Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/26e878ab Branch: refs/heads/master Commit: 26e878ab3a5b0844e09ce305fa07eb2d0ad93d41 Parents: 941b13f Author: Luc Maisonobe <l...@apache.org> Authored: Thu Sep 10 09:49:05 2015 +0200 Committer: Luc Maisonobe <l...@apache.org> Committed: Thu Sep 10 10:34:08 2015 +0200 ---------------------------------------------------------------------- src/changes/changes.xml | 3 +++ src/main/java/org/apache/commons/math4/util/FastMath.java | 4 ++-- src/test/java/org/apache/commons/math4/util/FastMathTest.java | 5 +++++ 3 files changed, 10 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-math/blob/26e878ab/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 91f3b24..443c5ce 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -54,6 +54,9 @@ If the output is not quite correct, check for invisible trailing spaces! </release> <release version="4.0" date="XXXX-XX-XX" description=""> + <action dev="luc" type="fix" issue="MATH-1272" due-to="Qualtagh"> + Fixed infinite loop in FastMath.pow(double, long) with Long.MIN_VALUE. + </action> <action dev="luc" type="fix" issue="MATH-1266"> <!-- backported to 3.6 --> Fixed split/side inconsistencies in BSP trees. </action> http://git-wip-us.apache.org/repos/asf/commons-math/blob/26e878ab/src/main/java/org/apache/commons/math4/util/FastMath.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/util/FastMath.java b/src/main/java/org/apache/commons/math4/util/FastMath.java index a4a9a1b..46c8752 100644 --- a/src/main/java/org/apache/commons/math4/util/FastMath.java +++ b/src/main/java/org/apache/commons/math4/util/FastMath.java @@ -1711,7 +1711,7 @@ public class FastMath { } /** Computes this^e. - * @param e exponent (beware, here it MUST be > 0) + * @param e exponent (beware, here it MUST be > 0; the only exclusion is Long.MIN_VALUE) * @return d^e, split in high and low bits * @since 4.0 */ @@ -1723,7 +1723,7 @@ public class FastMath { // d^(2p) Split d2p = new Split(full, high, low); - for (long p = e; p != 0; p >>= 1) { + for (long p = e; p != 0; p >>>= 1) { if ((p & 0x1) != 0) { // accurate multiplication result = result * d^(2p) using Veltkamp TwoProduct algorithm http://git-wip-us.apache.org/repos/asf/commons-math/blob/26e878ab/src/test/java/org/apache/commons/math4/util/FastMathTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/util/FastMathTest.java b/src/test/java/org/apache/commons/math4/util/FastMathTest.java index b95deba..bccd0e8 100644 --- a/src/test/java/org/apache/commons/math4/util/FastMathTest.java +++ b/src/test/java/org/apache/commons/math4/util/FastMathTest.java @@ -1232,6 +1232,11 @@ public class FastMathTest { Assert.assertTrue(Double.isInfinite(FastMath.pow(FastMath.scalb(1.0, 500), 4))); } + @Test(timeout=5000L) // This test must finish in finite time. + public void testIntPowLongMinValue() { + Assert.assertEquals(1.0, FastMath.pow(1.0, Long.MIN_VALUE), -1.0); + } + @Test public void testIncrementExactInt() { int[] specialValues = new int[] {