-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add compensated_add for floats #50774
Conversation
@rust-highfive seems to be down, so randomly assigning to a libs team member: r? @BurntSushi |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
Compensated summation is a great algorithm, but does this really need to be in std? It feels like once more than basic |
@scottmcm normally I'd agree, although based upon the criteria of:
I'd say that it's worthy of inclusion. I'd draw parallels to this and the Euclidean division/modulo functions which were recently added to libstd: it's nontrivial to implement, gets rid of a "weirdness" of the usual division/modulo, and helps act as a building block for more complicated things. I'd definitely be willing to do an RFC instead if you'd prefer once before such a thing is merged, though. |
Hmm, I'll try and tease out why I have a different reaction to this one compared to some others.
Whereas I feel like I'd still need to get out Numerical Recipes to be confident an algorithm is stable even if I was using I'm not on libs, though, so it's not up to me, and I don't know if an RFC would be desired. |
Ping from triage @BurntSushi! This PR needs your review. |
@pietroalbini I took myself off the PR review rotation because I just don't have the bandwidth to do it unfortunately. :-( |
Does a method like this have precedence for inclusion in the standard libraries of other languages? |
@alexcrichton Python offers I went back to look at the original justification for adding My main reasoning for offering the simple primitive through Kahan summing was that most people are not going to want the performance cut of a complicated algorithm like Offering simply a |
Hm so given the history with Python, is there a strong reason as to why this should be included in the standard library? It seems like with two possible options the way we'd prefer to go is to have both evolve on crates.io and if one gains enough traction we pull it into libstd? |
So, what should we do with this PR? Close it? |
Sounds like it! |
This method implements Kahan summation, which allows pretty good, mostly numerically stable summing of floats without any allocation or sorting, just tracking a single error term.
A "perfect" summation algorithm would likely be much less performant and be implemented as a method of
[f32]
and[f64]
, but for cases where people want[0.1; 10].iter().sum() == 1.0
, this is adequate.