Repository navigation
fix: match a numeric difference exactly at the tolerance - #668
pcbeingused33 wants to merge 6 commits into
Conversation
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
left a comment
There was a problem hiding this comment.
✅ 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,infand1e20tolerances 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-configand throughmodel_from_json:overall_scoregoes 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_toleranceorrelative_tolerance, so no other code path keeps the old boundary. - Full suite with
--all-extras: 6772 passed, 3 skipped, 1 xfailed.ruff checkis 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]→Fixedand links #648. The merge-base is the currentdev.
Non-blocking (low):
- A
Fraction(3, 10)tolerance orabsolute_tolerance=Truenow raisesInvalidOperation, where dev returned 1.0. Neither type matches thefloatannotation, and no loader produces either one. - Pre-existing, on a line this PR only renamed: when
num1 == 0andrelative_tolerance > 0, the code returns before it checksabsolute_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
left a comment
There was a problem hiding this comment.
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:
-
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")expecting0.0— would guard against a future change that widens the comparison (say, someone "simplifying" toround()or an epsilon). It fails on neither main nor the PR, so it's purely a fence. -
Decimal(str(x))on a caller-supplied float tolerance. A user who passesrelative_tolerance=1/3getsDecimal("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
left a comment
There was a problem hiding this comment.
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
-
The Comparators guide doesn't say that a difference equal to the tolerance matches.
docs/docs/Guides/Comparators/README.md:145says "match within tolerance" and the table rows at 167 and 168 give only examples, so the boundary this PR fixes isn't written down. TheAngleComparatorsection 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) |
-
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.
|
The Comparators guide now says a difference equal to the tolerance still matches. The intro uses "at most", as AngleComparator does, and the |
…dary # Conflicts: # CHANGELOG.md
Fixes #648
Description of Changes
NumericComparatornow treats a difference equal to the tolerance as within it, whatever binary value the tolerance float has.Changes Made
_numbers_equalreadsabsolute_tolerance(and itstolerancealias) andrelative_toleranceasDecimal(str(...)), the same way the compared values are read.tests/common/comparators/test_comparators.py, including a case just past the boundary that must score 0.0.CHANGELOG.mdentry under[Unreleased].Why These Changes Are Needed
The values were converted to
Decimalbut the tolerance stayed a float, so the exact decimal difference was compared against the float's binary value:absolute_tolerance=0.3failed8.0vs8.3while0.5passed2.0vs2.5.relative_tolerancehad the same problem (10vs13at0.3failed).Testing
The four boundary cases fail on
devand pass with the change. A case just past the boundary (absolute_tolerance=0.3,8.0vs8.3000000000000001) scores 0.0.ruff checkis clean on the comparator tests.Checklist
CHANGELOG.mdentry added under[Unreleased], or not needed becausenothing user-visible changed (see RELEASING.md)
This change was prepared with AI assistance (Claude Code).