-
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
Tracking Issue for int_abs_diff
#89492
Comments
For signed, I would prefer the name |
For |
But I found There are other methods that never return a negative value, and they still return the same type. For example |
Current state: u32vec.iter().fold(0, |acc, x| acc + (*x as i32 - median as i32).abs() as u32) Desired state: u32vec.iter().fold(0, |acc, x| acc + x.abs_diff(median)) // as per experimental API for u32 |
@shreepads I have done this: fn abs_diff(a: u32, b: u32) -> u32 {
if a > b {
a - b
} else {
b - a
}
} Somehow this is cleaner to me than casting to signed. I think it's more efficient too? Not as nice as if it were a built-in method, but still cleans up your fold, no? |
@tspiteri, if you want abs_diff to return an i64, what behavior do you suggest for the case when abs_diff can't fit in an i64? Panic? Change return type to result? I see no appealing options. (The difference between two i64s can be 64 bits long, and i64 has 63 bits for magnitude) |
@rrokkam I would expect the same overflow behaviour as abs itself. |
(FWIW, because I'm newish to Rust —) I very much disagree — it doesn't make sense to me to ship an overflowing API when a 100% correct + infallible algorithm exists. I'll concede some case for naming it |
Thanks @MarcelRobitaille , yes that is much better and would work till such time as a method is available in the std library - (there is already such a method in the experimental API for u32)... I was only hacking one of the Advent of Code puzzles (https://adventofcode.com/2021/day/7) so didn't mind the casting/ re-casting.. |
@rfcbot merge |
Team member @m-ou-se has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
Has a final decision on the name Of course, with no overflow there is no tricky case that requires this method, and |
The difference is that
|
🔔 This is now entering its final comment period, as per the review above. 🔔 |
This is for unsigned, while I was talking about signed. |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
@tspiteri Reading through this thread, I don't feel like there was a response explaining why your suggestion was not taken. In the spirit of collaboration, I'd like to attempt to fill in that gap according to my understanding (as an observer, not a decision maker) why Adding Anyway, just trying to help you see why I believe your naming suggestion wasn't selected. I could be wrong, in which case I welcome clarification. And, of course, you are welcome to continue preferring your own naming suggestion. I just wanted to make sure you felt heard. 😄 |
Sorry for the delayed response. We did quickly discuss this in a meeting, and concluded that we don't want a signed version of this, so there's no need for |
…oshtriplett Stabilize int_abs_diff in 1.60.0. FCP finished here: rust-lang#89492 (comment)
There's no better way to calculate the diff in the test right now, but abs_diff is on its way (rust-lang/rust#89492). We've also added an API specification to bpow and bpow_approx, detailing numerical limits and precision
Triage: the stabilization PR (#93735) has landed 🎉, closing as done. |
very funny, updated all to the recommended latest versions and got: following compiler advice I got here.. And noticed that for so many months, in spite of "JohnTitor closed this on 23 May" it has been still leaving gory traces... |
@catblue88 What's your rustc version? Note that it requires 1.60 or higher to use the methods on stable. |
hi John, |
Feature gate:
#![feature(int_abs_diff)]
This is a tracking issue for a method to compute the absolute difference between two integers.
Public API
This adds the method
T::abs_diff(other: T) -> U
to all built-in Rust integer types, both signed and unsigned, whereU
is the unsigned equivalent ofT
(or justT
itself it it already was unsigned). This method always computes the mathematical absolute difference between the two numbers correctly, without any risk of overflow. Because the output is an unsigned type the result is always representable.Steps / History
Note: scope has been expanded to implement
abs_diff
for all integers, not just unsigned integers.Unresolved Questions
The text was updated successfully, but these errors were encountered: