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-math.git


The following commit(s) were added to refs/heads/master by this push:
     new 8a940bfc5 Fix off-by-one rejecting trailing nan/infinity in 
parseNumber (#326)
8a940bfc5 is described below

commit 8a940bfc5dc1a83baf3fadcb4668acbf6591a85c
Author: Javid Khan <[email protected]>
AuthorDate: Wed Jun 10 00:10:41 2026 +0530

    Fix off-by-one rejecting trailing nan/infinity in parseNumber (#326)
    
    Add round-trip and imaginary-part special value parse tests
---
 .../commons/math4/legacy/util/CompositeFormat.java |  2 +-
 .../legacy/util/ComplexFormatAbstractTest.java     | 29 ++++++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git 
a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/util/CompositeFormat.java
 
b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/util/CompositeFormat.java
index 1390dc1a1..333496529 100644
--- 
a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/util/CompositeFormat.java
+++ 
b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/util/CompositeFormat.java
@@ -117,7 +117,7 @@ public final class CompositeFormat {
         final int n = sb.length();
         final int startIndex = pos.getIndex();
         final int endIndex = startIndex + n;
-        if (endIndex < source.length() &&
+        if (endIndex <= source.length() &&
             source.substring(startIndex, endIndex).compareTo(sb.toString()) == 
0) {
             ret = Double.valueOf(value);
             pos.setIndex(endIndex);
diff --git 
a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/util/ComplexFormatAbstractTest.java
 
b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/util/ComplexFormatAbstractTest.java
index a421348d6..d483982db 100644
--- 
a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/util/ComplexFormatAbstractTest.java
+++ 
b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/util/ComplexFormatAbstractTest.java
@@ -277,6 +277,35 @@ public abstract class ComplexFormatAbstractTest {
         Assert.assertEquals(expected, actual);
     }
 
+    @Test
+    public void testParseRealNan() {
+        // a real-only special value runs to the last character of the string
+        Complex expected = Complex.ofCartesian(Double.NaN, 0);
+        Assert.assertEquals(expected, complexFormat.parse("(NaN)"));
+        Assert.assertEquals(expected, 
complexFormat.parse(complexFormat.format(expected)));
+    }
+
+    @Test
+    public void testParseRealInfinity() {
+        Complex positive = Complex.ofCartesian(Double.POSITIVE_INFINITY, 0);
+        Assert.assertEquals(positive, complexFormat.parse("(Infinity)"));
+        Assert.assertEquals(positive, 
complexFormat.parse(complexFormat.format(positive)));
+        Complex negative = Complex.ofCartesian(Double.NEGATIVE_INFINITY, 0);
+        Assert.assertEquals(negative, complexFormat.parse("(-Infinity)"));
+        Assert.assertEquals(negative, 
complexFormat.parse(complexFormat.format(negative)));
+    }
+
+    @Test
+    public void testParseImaginarySpecialValue() {
+        // the special value sits in the imaginary part, ahead of the 
imaginary character
+        Complex nan = Complex.ofCartesian(1.23, Double.NaN);
+        Assert.assertEquals(nan, 
complexFormat.parse(complexFormat.format(nan)));
+        Complex positive = Complex.ofCartesian(1.23, Double.POSITIVE_INFINITY);
+        Assert.assertEquals(positive, 
complexFormat.parse(complexFormat.format(positive)));
+        Complex negative = Complex.ofCartesian(1.23, Double.NEGATIVE_INFINITY);
+        Assert.assertEquals(negative, 
complexFormat.parse(complexFormat.format(negative)));
+    }
+
     @Test
     public void testConstructorSingleFormat() {
         NumberFormat nf = NumberFormat.getInstance();

Reply via email to