Skip to content

fix: avoid overflow in NumericDiff for finite inputs - #237

Open
Deepanshu Pal (deepanshupal) wants to merge 4 commits into
braintrustdata:mainfrom
deepanshupal:fix/numericdiff-finite-overflow
Open

Deepanshu Pal (deepanshupal) wants to merge 4 commits into
braintrustdata:mainfrom
deepanshupal:fix/numericdiff-finite-overflow

Conversation

@deepanshupal

Copy link
Copy Markdown

NumericDiff can overflow its intermediate difference or magnitude sum even when both inputs are finite. Comparing the largest float with half that value returns a perfect score instead of 2/3. Comparing it with its negative returns NaN. Both Python and TypeScript have the same problem.

Scale both inputs by their largest absolute value before computing the normalized difference. This keeps the intermediates bounded without changing the formula for finite inputs, and preserves the zero/zero result.

Matching tests in both languages cover large positive and negative values, identical values, subnormals, zeros, and ordinary values. Three cases fail on the unchanged implementation and all eight pass with the fix. All offline tests pass (Python: 79; TypeScript: 78), and the JS build succeeds. Live API tests require credentials and were not run successfully.

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