-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
GH-100425: Improve accuracy of builtin sum() for float inputs #100426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
916ab7e
Neumaier compensated summation
rhettinger 787f2e5
Kahan summation
rhettinger bc308aa
Revert "Kahan summation"
rhettinger 0c2ba04
Restore the bogus sign tests
rhettinger 89e46c8
Add tests and comments
rhettinger a9cb7d2
Add tests and docs
rhettinger 839cc05
Add blurb
rhettinger 66db937
Remove out of date example for fsum()
rhettinger af7613e
Update floating point tutorial
rhettinger 4f4b731
Correct handling of overflowed or infinite sums
rhettinger 4da0b8f
Move local declarations inside the block where they are used.
rhettinger 6c7894c
For consistent results, apply compensation on the slow path as well.
rhettinger 3cf9536
Mark as an implementation detail.
rhettinger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Core and Builtins/2022-12-21-22-48-41.gh-issue-100425.U64yLu.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Improve the accuracy of ``sum()`` with compensated summation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why check for
c
being zero? Seems that unconditionally adding 0.0 (if it is 0) tof_result
would be cheap and harmless.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does avoid losing the sign on a negative zero result:
Admittedly that's a fairly niche corner case; I could live with the behaviour changing.
Thinking about corner cases, the compensation is also going to interfere with infinities and overflow. On this branch:
But on main we get infinities as expected:
We should try to keep the existing behaviour here. I think it should just be a matter of not adding the correction if it's an infinity or nan.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In commit 88bdb92 in 2019, Serhiy added these tests:
It looks like he thinks there should be an invariant that sign of the start value should be preserved when iterable sum is equal to zero or empty. This seems dubious to me, but I rolled with it and added the "if (c)" test to preserve the invariant.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's dubious: it's consistent with IEEE 754 rules that (under the default ties-to-even rounding mode) a sum of negative zeros is negative zero. It only affects the case where both the sum and the start value are negative zero; if the sum is positive zero it has no effect:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fine - but surely something so excruciatingly obscure deserves a comment 😜 .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Done.