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 1390dc1a16..333496529c 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 @@ private static Number parseNumber(final String source, final double value, 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 a421348d6f..d483982dbe 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 void testPaseNegativeInfinity() { 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();