Skip to content

Match native rounding for Python integer matrix scores - #497

Open
kokotatan wants to merge 1 commit into
rapidfuzz:mainfrom
kokotatan:fix/python-matrix-half-rounding
Open

kokotatan wants to merge 1 commit into
rapidfuzz:mainfrom
kokotatan:fix/python-matrix-half-rounding

Conversation

@kokotatan

Copy link
Copy Markdown

Integer dtype results from the Python cdist and cpdist implementations round exact halfway scores to even, while the native implementation uses std::llround and rounds them away from zero. For example, Levenshtein.normalized_similarity("ab", "ac") stored as uint8 is 0 in Python and 1 in C++.

This change uses the native rounding rule for Python integer matrices, including both symmetric cdist paths. Truncating before comparing the fractional remainder preserves exact integer scores and supports NumPy scalars, Decimal, and Fraction without introducing float conversion. Floating-point matrix output is unchanged.

Validation:

  • Before the fix: 26 new regression cases failed; 18 controls passed.
  • After the fix: full suite 475 passed on Windows, Python 3.13. Tests cover signed scaled scores, symmetric matrices, NumPy float32/float64, Decimal with FloatOperation trapped, Fraction, exact large integers, and existing non-finite error behavior.
  • Repository pre-commit hooks passed, including the all-files run after normalizing the local Windows shell-script checkout to LF. No shell scripts or build files are included in this PR.
  • Scorer typing checks, process mypy and pyright checks passed; pyrefly reported 100% type coverage.

The tests used current Python source with the native modules from the matching 3.14.6 Windows wheel as the C++ reference. A fresh native build was not performed because the local environment has no C++ compiler. For the scorer typing script, its bare python subprocess command was routed to the active virtualenv interpreter to avoid Windows selecting the base interpreter.

AI assistance: implemented and tested with OpenAI Codex (GPT-6), followed by independent agent review and another review of the resulting correction. This PR is submitted under the account owner's explicit authorization.

Assisted-by: OpenAI GPT-6 <noreply@openai.com>

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant