Skip to content

[FIX,TEST] FuzzyStringComparator: NaN is not equal to everything (#9859) - #9861

Merged
timosachsenberg merged 4 commits into
developfrom
fix/fuzzy-comparator-nan
Aug 6, 2026
Merged

timosachsenberg merged 4 commits into
developfrom
fix/fuzzy-comparator-nan

Conversation

@timosachsenberg

@timosachsenberg timosachsenberg commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #9859.

The defect

Every comparison against NaN is false, so nothing in FuzzyStringComparator's number-vs-number branch could reject one:

  • :328 if (element_1_.number == element_2_.number) — false, so no early accept;
  • :347 is_absdiff_small_ = (absdiff <= absdiff_max_allowed_) — absdiff is NaN, so false;
  • :376 double ratio = element_1_.number / element_2_.number; — NaN;
  • :378 if (ratio < 0) — false, so the "different signs" failure is skipped;
  • :387 if (ratio < 1) — false, so the reciprocal is not taken;
  • :396 if (ratio > ratio_max_) — false, and this is the sole gate to reportFailure_.

Control reached the end of the branch and the pair was accepted. nan therefore compared equal to any number, and +inf compared equal to -inf because their ratio is NaN too.

The consequence is not subtle: a value that regressed to NaN was invisible to every file comparison in the suite — the 778 FuzzyDiff comparisons in the TOPP tests and TEST_FILE_EQUAL in the class tests — whatever the other file held. That is precisely the direction a real regression travels: the tool emits NaN, the reference holds a legitimate number, the test stays green.

The fix

Non-finite pairs are decided by category before the arithmetic runs:

in1 in2 before after
nan nan equal equal (unchanged)
inf inf equal equal (unchanged)
-inf -inf equal equal (unchanged)
nan 5.0 equal differs
5.0 nan equal differs
nan ±inf equal differs
inf -inf equal differs
inf / -inf 5.0 differs differs (unchanged)
nan abc differs differs (unchanged)
1.0 2.0 differs differs (unchanged)

Two NaNs are equal on purpose — a reference may legitimately contain nan, and they never reach the == fast path since NaN != NaN.

Deliberately surgical. An infinity against a finite number is ordered and was already reported correctly — by the ratio test for +inf, by the sign test for -inf — so it keeps its existing path and message. Only the outcomes that were wrong change, which also leaves the failure text and the running-max statistics untouched for everything that already worked.

Blast radius: none

Full suite: 2811 tests, no new failures. The 9 that fail in this environment reproduce identically with the fix reverted — 6× CometAdapter version string, PercolatorAdapter binary numerics, Doxygen HAVE_DOT, and ThermoRawFile_test which needs DOTNET_ROOT. I verified this by reverting the change, rebuilding, and re-running exactly those tests rather than assuming.

The four reference files carrying nan on the reference side still pass, because those are nan-against-nan and remain equal under the new rule:

file nan occurrences
MzTabExporter_3_output.mzTab 1268
TextExporter_2_consensus_tsv.tsv 387
TextExporter_2_consensus_features.wsv 258
TextExporter_7_consensus_tsv.tsv 33

So the hole was real, but it is not currently masking a live regression on this platform and configuration. That is worth stating plainly: the value of this change is preventing the next one, not repairing an existing wrong result.

Tests

The new class-test section was confirmed to fail without the fix, at exactly the six assertions corresponding to the defect. It does not fail at nan vs 0.0 (already caught pre-fix via the zero branch) or at an infinity vs a finite number (already ordered correctly) — which is the evidence that the change is as narrow as claimed.

It also pins that a NaN elsewhere on a line does not make the rest of that line permissive.

Caveat I could not settle locally

Non-finite formatting is platform-dependent — nan vs -nan vs 1.#IND — which is why SVOutStream::writeValueOrNan exists at all. Everything above is Linux/gcc. If a Windows or macOS runner formats a non-finite value differently from what a reference file records, this change will surface it as a failure where it was previously swallowed. That would be the fix working as intended, but it is the most likely source of CI noise on this PR and worth watching.

Related

🤖 Generated with Claude Code

https://claude.ai/code/session_01VcFtJVe1PcMsBzMnnnpWBJ

Summary by CodeRabbit

  • Bug Fixes

    • Improved comparisons involving special numeric values such as NaN and infinity.
    • Prevented NaN and opposite infinities from being incorrectly treated as equal to ordinary numbers.
    • Preserved expected tolerance behavior for finite numeric comparisons.
  • Tests

    • Added coverage for matching and mismatching NaN and infinity values, mixed finite/non-finite comparisons, and tolerance handling.
  • Documentation

    • Updated the changelog with details of the comparison fix.

Fixes #9859.

Every comparison against NaN is false, so nothing in the number-vs-number branch
could reject one: 'absdiff > absdiff_max_', 'ratio < 0', 'ratio < 1' and finally
'ratio > ratio_max_' -- the sole gate to reportFailure_ -- were all false, and
the pair fell through to the end of the branch and was accepted. 'nan' therefore
compared equal to ANY number, and +inf compared equal to -inf because their ratio
is NaN as well.

The consequence is not subtle: a value that regressed to NaN was invisible to
every file comparison in the suite -- the 778 FuzzyDiff comparisons of the TOPP
tests and TEST_FILE_EQUAL in the class tests -- whatever the other file held.
That is the direction a real regression travels: the tool emits NaN, the
reference holds a legitimate number, and the test stays green.

Non-finite pairs are now decided by category before the arithmetic runs: the same
non-finite value on both sides is equal (a reference may legitimately contain
'nan', and two NaNs never reach the equality fast path because NaN != NaN), and
anything else is reported.

Deliberately surgical: an infinity against a finite number is ordered and was
already reported correctly -- by the ratio test for +inf, by the sign test for
-inf -- so it keeps its existing path and message. Only the outcomes that were
wrong change, which also keeps the failure text and the running max statistics
unchanged for everything that already worked.

Full suite: 2811 tests, no new failures. The 9 that fail here are environmental
and reproduce identically without this change (6x CometAdapter version string,
PercolatorAdapter binary numerics, Doxygen HAVE_DOT, and ThermoRawFile which
needs DOTNET_ROOT). In particular the four reference files that carry 'nan' on
the reference side -- MzTabExporter_3_output.mzTab with 1268 of them,
TextExporter_2/_7 with 678 between them -- still pass, because those are
nan-against-nan and remain equal under the new rule. So the hole was real but is
not currently masking a live regression on this platform.

The new class-test section was confirmed to fail without the fix, at exactly the
six assertions that correspond to the defect. It does NOT fail at 'nan' vs '0.0'
or at an infinity vs a finite number, because those were already reported
correctly -- which is the evidence that the change is as narrow as intended.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01VcFtJVe1PcMsBzMnnnpWBJ
@coderabbitai

coderabbitai Bot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d0d61c99-dde8-42c0-a032-b4ce38121386

📥 Commits

Reviewing files that changed from the base of the PR and between 2d291a7 and 1c9c4b1.

📒 Files selected for processing (1)
  • CHANGELOG
🚧 Files skipped from review as they are similar to previous changes (1)
  • CHANGELOG

📝 Walkthrough

Walkthrough

FuzzyStringComparator now handles NaN and infinity comparisons explicitly. Matching non-finite values compare equal, while mismatched categories fail. Regression tests cover these cases and preserve existing finite-number tolerance behavior.

Changes

Non-finite comparison handling

Layer / File(s) Summary
Classify non-finite numeric comparisons
src/openms/source/CONCEPT/FuzzyStringComparator.cpp, src/tests/class_tests/openms/source/FuzzyStringComparator_test.cpp, CHANGELOG
The comparator handles matching NaNs, NaN mismatches, and infinity sign mismatches before tolerance checks. Tests cover non-finite values and ordinary numeric tolerance. The changelog records the behavior.

Possibly related PRs

  • OpenMS/OpenMS#9860: Documents the impact of NaN comparison handling on ProteinQuantifier regression tests.

Suggested reviewers: cbielow

Poem

A rabbit checks each number’s face,
NaN meets NaN in equal space.
Opposite infinities part,
Tests guard every numeric heart.
The changelog hops behind.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the FuzzyStringComparator fix and its NaN comparison behavior.
Linked Issues check ✅ Passed The changes address issue #9859 by classifying NaN and infinity values before comparison and adding regression tests.
Out of Scope Changes check ✅ Passed The changelog, comparator logic, and regression tests directly support issue #9859 and contain no unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/fuzzy-comparator-nan

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/openms/source/CONCEPT/FuzzyStringComparator.cpp`:
- Around line 354-359: Update the infinite-value classification in
FuzzyStringComparator.cpp around the existing comparison logic to reject any
pair where either operand is infinite, after equality and NaN checks, while
preserving the existing differing-sign message for two infinities. In
src/tests/class_tests/openms/source/FuzzyStringComparator_test.cpp lines
473-476, add the reverse-order assertion that equal("x 5.0", "x -inf") is false.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 4a6c24e2-c594-463e-8fea-0d7b623cfb04

📥 Commits

Reviewing files that changed from the base of the PR and between 6d09f84 and 6295c55.

📒 Files selected for processing (3)
  • CHANGELOG
  • src/openms/source/CONCEPT/FuzzyStringComparator.cpp
  • src/tests/class_tests/openms/source/FuzzyStringComparator_test.cpp

@timosachsenberg
timosachsenberg requested a review from cbielow August 5, 2026 06:52

@timosachsenberg timosachsenberg left a comment

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.

looks correct to me

@timosachsenberg
timosachsenberg merged commit 7a7aa8f into develop Aug 6, 2026
11 checks passed
@timosachsenberg
timosachsenberg deleted the fix/fuzzy-comparator-nan branch August 6, 2026 05:33
timosachsenberg pushed a commit that referenced this pull request Aug 7, 2026
Three CHANGELOG entries already document their change in full but were
missing the trailing PR citation: consensusXML protein-group quantity
round-tripping (#9844), the FuzzyStringComparator NaN-equality fix
(#9859, #9861), and the "five more Parquet writers left a partial file
behind" fix (#9862).

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01YAD9pD1nBputTVhzddmCj5
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.

FuzzyStringComparator accepts any comparison involving NaN, so a value regressing to NaN is invisible to 778 TOPP reference tests

1 participant