Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ Each release links to full notes on the

### Fixed

- **`NumericComparator` matches a difference exactly at its tolerance.**
Upgrade note: scores move for a difference exactly at the tolerance, which
now matches. 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)).
- A `field_comparisons` row that meets its threshold only through the `1e-10`
allowance now reads `within threshold tolerance (<score> vs <threshold>)` for a
top-level or nested-model field too, where it read `above threshold` (or
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/Guides/Comparators/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class Contact(StructuredModel):

### NumericComparator

Extracts numeric values from strings or numbers and compares them with configurable tolerance. Handles currency symbols, commas, and accounting notation (e.g., `(123)` for negative values). Returns 1.0 if the numbers match within tolerance, 0.0 otherwise.
Extracts numeric values from strings or numbers and compares them with configurable tolerance. Handles currency symbols, commas, and accounting notation (e.g., `(123)` for negative values). Returns 1.0 if the difference is at most the tolerance, 0.0 otherwise.

String values may use decimal digits in other scripts: `"١٬٢٥٠٫٥٠"` reads 1250.50, as `"1,250.50"` does. A value whose digits are all outside ASCII reads as no number unless it holds one unambiguous number. A value that also has ASCII digits reads its ASCII number and ignores its other-script digits (`"1,250.00 (١٬٢٥٠٫٠٠)"` reads 1250.00), but a number whose digits switch script, such as `"١٢٥٠.50"`, reads as no number unless it is the same amount written twice. A minus sign such as U+2212 marks a negative when it opens the value or follows a currency symbol (`"−1250"`, `"$−1250"`), as do accounting parentheses that are fullwidth or have spaces around them (`"(1250)"`, `" (1250) "`); `"(-1250)"` reads -1250. A compact form such as `"١٫٣ ألف"` reads the number shown (1.3), as `"1.3K"` does; one with a CJK numeral (`"12万円"`) reads as no number. Declare the field `str`: a `float`, `int` or `Decimal` field rejects `"١٬٢٥٠٫٥٠"` before the comparator runs.

Expand All @@ -164,8 +164,8 @@ class Invoice(StructuredModel):
| Parameter | Default | Description |
|---|---|---|
| `threshold` | `1.0` | Similarity threshold for binary classification |
| `relative_tolerance` | `0.0` | Relative tolerance (e.g., `0.1` = 10%) |
| `absolute_tolerance` | `0.0` | Absolute tolerance (e.g., `0.01` for cents) |
| `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) |
| `tolerance` | `None` | Alias for `absolute_tolerance` (backward compatibility) |

---
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
21 changes: 21 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,26 @@ 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_difference_just_past_tolerance_does_not_match(self):
"""A difference just past the tolerance stays a miss (#648)."""
score = NumericComparator(absolute_tolerance=0.3).compare(
8.0, "8.3000000000000001"
)
assert score == 0.0

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