-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 non-panicking abs() functions to all signed integer types. #1678
Conversation
wrapping_abs
function to all signed integer types.
An alternative could be Edit: I misread the proposal and thought it was suggesting that e.g. |
Obtaining the absolute value is going to always result in a positive number, so why would you not want to store that in an unsigned type? In any case, one example where this is useful for converting a number to a sign-and-magnitude representation: (signed.is_negative(),signed.wrapping_abs() as u64) The alternative you mention is not really an alternative but it is something that could be added to this RFC. |
Added |
wrapping_abs
function to all signed integer types.
Rendered link seems to be broken |
Earlier proposal for reference: Change abs() to return unsigned integers |
Fixed |
This RFC proposes to add the following: | ||
|
||
```rust | ||
pub fn checking_abs(self) -> Option<Self> { |
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.
checked_abs
, not checking
, would seem to be more consistent with existing conventions.
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.
Yes, of course.
Do all these have concrete use cases or are they just here to provide symmetry to other numeric methods? ISTM like |
See above for a usecase for wrapping. I don't know of one for overflowing. |
🔔 This RFC is now entering its week-long final comment period 🔔 The libs team is leaning towards merging this as is, and we'd also be willing to just take a PR for this as well if you'd prefer to take that route @jethrogb :) |
Thanks @jethrogb! In that case I'll just close this in favor of that. |
Rendered