Repository navigation
Closed
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE][DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.This issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Description
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, where U is the unsigned equivalent of T (or just T 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
- Issue: Add a method for computing the absolute difference between unsigned integers #62111
Note: scope has been expanded to implementabs_difffor all integers, not just unsigned integers. - Implementation: Added abs_diff for integer types. #88780
- Final comment period (FCP)
- Stabilization PR: Stabilize int_abs_diff in 1.60.0. #93735
Unresolved Questions
- None yet.
Reacted by Stepan Koltsov, Caio, Glitchy Tozier and Kirk Baird
Reacted by Andrzej Głuszak, Peter Mikola, Erik Brinkman, Jake Goulding, shreepads, Maciej Goszczycki, Guilhem Saurel, nhamovitz, Cryptjar, Ludwig Stecher and 3 more
Metadata
Metadata
Assignees
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE][DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.This issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Activity
tspiteri commentedon Oct 14, 2021
For signed, I would prefer the name
unsigned_abs_diffas it returns a different type, making it consistent withunsigned_abs.orlp commentedon Oct 14, 2021
For
unsigned_absa distinction needed to be made with a pre-existingabsfunction. We don't have to worry about that backwards compatibility here.tspiteri commentedon Oct 14, 2021
But I found
i64::abs_diffreturningu64very surprising: a method that operates on two numbers to give a new number silently changes the type. The only otheri64method returningu64isunsigned_abs, and its name clearly indicates that.There are other methods that never return a negative value, and they still return the same type. For example
i64::absitself returnsi64, notu64, even though it can overflow as well on signed (although only for one value).shreepads commentedon Dec 14, 2021
Current state:
Desired state:
MarcelRobitaille commentedon Dec 18, 2021
@shreepads I have done this:
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?
rrokkam commentedon Dec 21, 2021
@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)
tspiteri commentedon Dec 21, 2021
@rrokkam I would expect the same overflow behaviour as abs itself.
nhamovitz commentedon Dec 21, 2021
(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
unsigned_abs_diffto highlight that the type is changing (although IMO that's what the function signature + your tooling is there for), but allowing overflows seems bad.shreepads commentedon Dec 21, 2021
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..
m-ou-se commentedon Jan 9, 2022
@rfcbot merge
rfcbot commentedon Jan 9, 2022
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.
5 remaining items
tspiteri commentedon Feb 5, 2022
This is for unsigned, while I was talking about signed.
rfcbot commentedon Feb 5, 2022
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.
CleanCut commentedon Feb 6, 2022
@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
abs_diffis an appropriate name!Adding
unsignedanywhere in a method name (usually in the suffix) is helpful to distinguish between a pair of analogous functions for both signed and unsigned flavors of the same operation. Otherwise, in general it is redundant to include signedness in the method name. You can see this (implicit?) naming policy if you examine the long list of methods oni32. Every method that includesunsignedin the name has a corresponding method with a signed flavor (which simply omitunsignedrather than containingsigned). The rest of the methods do not mention the signedness in the name of the method at all, including the other almost two dozen methods which return unsigned values other than the one you cited (which does have an analogous signed method).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. 😄
m-ou-se commentedon Feb 7, 2022
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
unsignedin the name. See @CleanCut's comment, who worded it better than I could have. :)Rollup merge of rust-lang#93735 - m-ou-se:stabilize-int-abs-diff, r=j…
Fix precision in tests, add docs & comments
JohnTitor commentedon May 23, 2022
Triage: the stabilization PR (#93735) has landed 🎉, closing as done.
catblue88 commentedon Aug 12, 2022
very funny, updated all to the recommended latest versions and got:
error[E0658]: use of unstable library feature 'int_abs_diff'
--> ... solana-core-1.11.5/src/serve_repair.rs:649:48
|
649 | let time_diff_ms = timestamp().abs_diff(header.timestamp);
| ^^^^^^^^
|
= note: see issue #89492 #89492 for more information
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...
JohnTitor commentedon Aug 12, 2022
@catblue88 What's your rustc version? Note that it requires 1.60 or higher to use the methods on stable.
catblue88 commentedon Aug 12, 2022
hi John,
I went out to let some steam off :-)
Yes, in spite of my effort to update to 1.63.0 it was still 1.59.0 I used.
You were right, it builds up now..
Thanks a lot for you lightning fast reaction..