Skip to content

fix: match a numeric difference exactly at the tolerance - #668

Open
pcbeingused33 wants to merge 6 commits into
awslabs:devfrom
pcbeingused33:fix/648-tolerance-boundary
Open

pcbeingused33 wants to merge 6 commits into
awslabs:devfrom
pcbeingused33:fix/648-tolerance-boundary

Conversation

@pcbeingused33

@pcbeingused33 pcbeingused33 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #648

Description of Changes

NumericComparator now treats a difference equal to the tolerance as within it, whatever binary value the tolerance float has.

Changes Made

  • _numbers_equal reads absolute_tolerance (and its tolerance alias) and relative_tolerance as Decimal(str(...)), the same way the compared values are read.
  • Boundary tests in tests/common/comparators/test_comparators.py, including a case just past the boundary that must score 0.0.
  • The Comparators guide states that a difference equal to the tolerance still matches.
  • A CHANGELOG.md entry under [Unreleased].

Why These Changes Are Needed

The values were converted to Decimal but the tolerance stayed a float, so the exact decimal difference was compared against the float's binary value: absolute_tolerance=0.3 failed 8.0 vs 8.3 while 0.5 passed 2.0 vs 2.5. relative_tolerance had the same problem (10 vs 13 at 0.3 failed).

Testing

  • Unit tests added/updated
  • Integration tests added/updated
  • Manual testing completed
  • All existing tests pass

The four boundary cases fail on dev and pass with the change. A case just past the boundary (absolute_tolerance=0.3, 8.0 vs 8.3000000000000001) scores 0.0. ruff check is clean on the comparator tests.

Checklist

  • Code follows the project's coding standards
  • Self-review of code completed
  • Documentation updated (if applicable)
  • CHANGELOG.md entry added under [Unreleased], or not needed because
    nothing user-visible changed (see RELEASING.md)
  • Tests added/updated for new functionality
  • All CI checks are passing

This change was prepared with AI assistance (Claude Code).

Compare against Decimal(str(tolerance)) so the boundary no longer depends on the float's binary value.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@vawsgit vawsgit 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.

✅ LGTM!

I measured this PR at head c20ddcd and found no medium or high findings. It delivers everything #648 asks for: the tolerances are converted with Decimal(str(tol)) in all three comparisons in NumericComparator._numbers_equal, and a boundary test is added.

Scores on origin/dev and on this PR. Each run printed which source tree it imported.

Case dev PR
absolute_tolerance=0.3, 8.0 vs 8.3 0.0 1.0
tolerance=0.3 alias, 8.0 vs 8.3 0.0 1.0
relative_tolerance=0.3, 10 vs 13 0.0 1.0
relative_tolerance=0.3, 0 vs 0.3 (the num1 == 0 branch) 0.0 1.0
String inputs "8.0" vs "$8.30", abs 0.3 0.0 1.0
np.float64(0.3) tolerance, and np.float32 values 0.0 1.0
abs 0.3, 8.0 vs 8.3000001 (just past the boundary) 0.0 0.0

Other checks, all passed:

  • Negative values, zero tolerance, int, Decimal, inf and 1e20 tolerances give the same results on both trees. Nothing that matched before fails now.
  • End to end through a JSON Schema with x-aws-stickler-comparator-config and through model_from_json: overall_score goes from 0.0 on dev to 1.0 on this PR for the 8.0/8.3 and 10/13 cases.
  • No other code reads absolute_tolerance or relative_tolerance, so no other code path keeps the old boundary.
  • Full suite with --all-extras: 6772 passed, 3 skipped, 1 xfailed. ruff check is clean.
  • Mutation tests: I reverted each of the three changed comparisons one at a time, and each revert fails its own case in the new parametrized test.
  • The CHANGELOG entry is under [Unreleased] → Fixed and links #648. The merge-base is the current dev.

Non-blocking (low):

  • A Fraction(3, 10) tolerance or absolute_tolerance=True now raises InvalidOperation, where dev returned 1.0. Neither type matches the float annotation, and no loader produces either one.
  • Pre-existing, on a line this PR only renamed: when num1 == 0 and relative_tolerance > 0, the code returns before it checks absolute_tolerance.

Note for maintainers: CI has not run yet. The four fork workflows show action_required, so a maintainer must approve them in the Actions tab.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@mangeshraut712 mangeshraut712 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.

Tested the PR head (34ccc7d9c) in a clean worktree and probed both sides of the boundary.

Result: 12/12 Numeric tests pass, and the 4 new parametrized cases all fail on main with assert 0.0 == 1.0, so the regression test has teeth.

The fix is internally consistent in a way that's worth stating explicitly: _extract_number (line 586) already does Decimal(str(value)), so the values are read as written. This PR makes the tolerance follow the same rule, so Decimal("8.3") - Decimal("8.0") == Decimal("0.3") exactly. Before, the comparison was written-vs-binary, and since Decimal(0.3) is 0.29999999999999998889…, the exact boundary could never match.

Boundary → 1.0 (all hold): absolute_tolerance=0.3 on 8.0/8.3, tolerance=0.3 on 8.0/8.3, tolerance=0.1 on 0/0.1, relative_tolerance=0.3 on 10/13, negative -8.0/-8.3, string inputs with trailing zeros ("8.3000") and scientific notation ("8.3E0"), tolerance passed as Decimal("0.3") or 3e-1, and 0.1+0.2 vs 0.3 (which str() reads as "0.30000000000000004" — a diff of 4e-17, well inside 0.1).

One unit beyond → 0.0 — the fix doesn't over-widen:

{'absolute_tolerance': 0.3}  ('8.0', '8.3000000000000001')          -> 0.0
{'absolute_tolerance': 0.1}  ('1.0', '1.10000000000000001')         -> 0.0
{'relative_tolerance': 0.1}  ('100', '110.00000000000001')          -> 0.0
{'absolute_tolerance': 0.1}  ('0',   '0.100000000000000005')        -> 0.0

Large values behave too: absolute_tolerance=1 on 1e14 vs 1e14+1 → 1.0; relative_tolerance=0.01 on 1e15 vs 1.01e15 → 1.0.

Two small suggestions, neither blocking:

  1. A beyond-boundary case in the parametrize list. The current 4 cases pin the "exactly at" side. One row on the other side — e.g. ({"absolute_tolerance": 0.3}, 8.0, "8.3000000000000001") expecting 0.0 — would guard against a future change that widens the comparison (say, someone "simplifying" to round() or an epsilon). It fails on neither main nor the PR, so it's purely a fence.

  2. Decimal(str(x)) on a caller-supplied float tolerance. A user who passes relative_tolerance=1/3 gets Decimal("0.3333333333333333") — the written repr, which is the right "as the user wrote it" reading. Just noting it's the intended semantics and matches the values, not a problem.

The CHANGELOG entry is clear and in house style. LGTM — this is the right fix for #648.

@sromoam sromoam 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.

Thanks @pcbeingused33, this does what #648 asks. At head 34ccc7d against current dev (ce0f710), the four new cases go from 0.0 to 1.0 (also through from_json_schema() and model_from_json() for 8.0 vs 8.3 and 10 vs 13), 8.0 vs 8.31 still scores 0.0, and the full suite passes locally. The dev merge since vawsgit's approval leaves your diff unchanged.

Smaller

  1. The Comparators guide doesn't say that a difference equal to the tolerance matches. docs/docs/Guides/Comparators/README.md:145 says "match within tolerance" and the table rows at 167 and 168 give only examples, so the boundary this PR fixes isn't written down. The AngleComparator section just below says "at most" for the same idea. Something like:

    | `relative_tolerance` | `0.0` | Largest relative difference that still matches (e.g., `0.1` = 10%) |
    | `absolute_tolerance` | `0.0` | Largest absolute difference that still matches (e.g., `0.01` for cents) |
  2. Optional: +1 to mangeshraut712's first suggestion, a row just past the boundary that must score 0.0, so a later change that widens the comparison fails a test.

One heads-up: we're not merging anything outside the 1.1.0 milestone until 1.1.0 lands, so this will wait until after that even once it's ready.

The Comparators guide now says the boundary is inclusive, and a case
just past absolute tolerance must score 0.0.
@pcbeingused33

Copy link
Copy Markdown
Contributor Author

The Comparators guide now says a difference equal to the tolerance still matches. The intro uses "at most", as AngleComparator does, and the relative_tolerance and absolute_tolerance rows use the wording from the review. Added the case just past the boundary (absolute_tolerance=0.3, 8.0 vs 8.3000000000000001) expecting 0.0. Those five comparator cases pass on this branch. Understood that this waits until 1.1.0.

@sromoam sromoam added the anytime Non-breaking improvement off the release critical path: merge whenever it's ready label Dec 4, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

anytime Non-breaking improvement off the release critical path: merge whenever it's ready

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: NumericComparator passes or fails a difference exactly at absolute_tolerance depending on the float

4 participants