Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ Each release links to full notes on the

### Fixed

- **`NumericComparator` matches a difference exactly at its tolerance.** The
exact decimal difference was compared against the float's binary value, so
`absolute_tolerance=0.3` failed `8.0` against `8.3` while `0.5` passed `2.0`
against `2.5`. `absolute_tolerance`, `tolerance` and `relative_tolerance` are
now read as written
([#648](https://github.com/awslabs/stickler/issues/648)).
- **`to_stickler_config()` exports a tuple field as a tuple.** It was written
as `"str"`, so the model rebuilt by `model_from_json()` rejected the original
value. Tuples of primitives keep their element types (`Tuple[float, float]`);
Expand Down
10 changes: 7 additions & 3 deletions src/stickler/comparators/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,18 +653,22 @@ def _numbers_equal(self, num1: Decimal, num2: Decimal) -> bool:

# Check with relative tolerance
if self.relative_tolerance > 0:
# Read the tolerance as written, as the values are: the float 0.3 is
# slightly below Decimal("0.3"), which would fail an exact boundary.
relative_tolerance = Decimal(str(self.relative_tolerance))

# Handle zero case
if num1 == 0:
return abs(num2) <= self.relative_tolerance
return abs(num2) <= relative_tolerance

# Calculate relative difference using num1 as base
relative_diff = abs(num1 - num2) / abs(num1)
if relative_diff <= self.relative_tolerance:
if relative_diff <= relative_tolerance:
return True

# Check with absolute tolerance
if self.absolute_tolerance > 0:
if abs(num1 - num2) <= self.absolute_tolerance:
if abs(num1 - num2) <= Decimal(str(self.absolute_tolerance)):
return True

return False
Expand Down
14 changes: 14 additions & 0 deletions tests/common/comparators/test_comparators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
to ensure they work correctly and maintain compatibility with existing code.
"""

import pytest

from stickler.comparators import (
LevenshteinComparator,
Expand Down Expand Up @@ -108,6 +109,19 @@ def test_negative_numbers(self):
assert self.comparator.compare("-123", -123) == 1.0
assert self.comparator.compare("(123)", -123) == 1.0

@pytest.mark.parametrize(
"kwargs, first, second",
[
({"absolute_tolerance": 0.3}, 8.0, 8.3),
({"tolerance": 0.3}, 8.0, 8.3),
({"relative_tolerance": 0.3}, 10, 13),
({"relative_tolerance": 0.3}, 0, 0.3),
],
)
def test_difference_equal_to_tolerance_matches(self, kwargs, first, second):
"""A difference exactly at the tolerance is within it (#648)."""
assert NumericComparator(**kwargs).compare(first, second) == 1.0

def test_tolerance(self):
"""Test that tolerance is respected."""
# Without tolerance
Expand Down