Skip to content

fix off-by-one rejecting trailing nan/infinity in parseNumber#326

Merged
aherbert merged 2 commits into
apache:masterfrom
dxbjavid:parsenumber-trailing-special-value
Jun 9, 2026
Merged

fix off-by-one rejecting trailing nan/infinity in parseNumber#326
aherbert merged 2 commits into
apache:masterfrom
dxbjavid:parsenumber-trailing-special-value

Conversation

@dxbjavid

@dxbjavid dxbjavid commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

CompositeFormat.parseNumber checks endIndex < source.length() before matching a special value like (NaN) or (Infinity), so a special-value token that runs to the final character of the input is never recognised. This breaks the format/parse round trip for a real-only NaN or infinity in ComplexFormat, where parse throws MathParseException on the very string format produced. Widening the comparison to endIndex <= source.length() lets a special value ending the input parse, which also covers RealVectorFormat and RealMatrixFormat as they share the same helper.

public void testParseRealInfinity() {
Complex positive = Complex.ofCartesian(Double.POSITIVE_INFINITY, 0);
Assert.assertEquals(positive, complexFormat.parse("(Infinity)"));
Complex negative = Complex.ofCartesian(Double.NEGATIVE_INFINITY, 0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should test the round-trip:

Assert.assertEquals(positive, complexFormat.parse(complexFormat.format(positive)));
Assert.assertEquals(negative, complexFormat.parse(complexFormat.format(negative)));

Parsing should also test special values in the imaginary part:

Complex.ofCartesian(1.23, Double.NaN);
Complex.ofCartesian(1.23, Double.POSITIVE_INFINITY);
Complex.ofCartesian(1.23, Double.NEGATIVE_INFINITY);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. The infinity test now checks the round trip for both signs, and I added testParseImaginarySpecialValue covering NaN, +Infinity and -Infinity in the imaginary part (parsed back from format output). All pass with the patch.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.15%. Comparing base (f554608) to head (e0bee2b).
⚠️ Report is 323 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #326      +/-   ##
============================================
+ Coverage     86.54%   87.15%   +0.61%     
+ Complexity     9787       89    -9698     
============================================
  Files           532      499      -33     
  Lines         35516    33463    -2053     
  Branches       6194     5833     -361     
============================================
- Hits          30738    29166    -1572     
+ Misses         3518     3174     -344     
+ Partials       1260     1123     -137     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@aherbert aherbert merged commit 8a940bf into apache:master Jun 9, 2026
9 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants