Skip to content

Add saturating_abs and saturating_neg #59983

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
t-rapp opened this issue Apr 15, 2019 · 6 comments · Fixed by #71886
Closed

Add saturating_abs and saturating_neg #59983

t-rapp opened this issue Apr 15, 2019 · 6 comments · Fixed by #71886
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@t-rapp
Copy link
Contributor

t-rapp commented Apr 15, 2019

It would be great to have saturating_abs and saturating_neg functions for signed integer types. The current work-around is using a pattern like checked_$opr().unwrap_or($typ::max_value()) but having explicit functions would lift the corner-case knowledge "burden" from the user.

@hellow554
Copy link
Contributor

What do you expect them to return? Just 0 for the two values that can't be expressed? IMHO you should prefer the check_$opr instead and explicitly handle that.

@jonas-schievink jonas-schievink added C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Apr 15, 2019
@t-rapp
Copy link
Contributor Author

t-rapp commented Apr 15, 2019

The abs and neg functions both have a corner case when the negative minimum value is used (e.g. -128 for type i8). The result cannot be represented within the bounds of the output type.

The saturating_abs / saturating_neg variants shall return a best-effort value (like 127 in case of -128 i8 input), similar to the existing saturating_add/sub/mul/pow functions.

assert_eq!(127i8.saturating_abs(), 127);
assert_eq!(-127i8.saturating_abs(), 127);
assert_eq!(-128i8.saturating_abs(), 127);

assert_eq!(127i8.saturating_neg(), -127);
assert_eq!(-127i8.saturating_neg(), 127);
assert_eq!(-128i8.saturating_neg(), 127);

@scottmcm
Copy link
Member

These seems reasonable. I'd suggest just making a PR with them unstable and seeing what libs says.

By using 0.saturating_sub(x) they'll automatically get the new saturating implementation, too, which should be better for optimization and codegen than the checked_ approach.

@t-rapp
Copy link
Contributor Author

t-rapp commented Apr 16, 2019

Haven't done a PR for Rust yet so will take a look at the contribution guidelines and try to get along.

bors added a commit that referenced this issue Apr 25, 2019
Implement saturating_abs() and saturating_neg() functions for signed integer types

Similar to wrapping_abs() / wrapping_neg() functions but saturating at the numeric bounds instead of wrapping around. Complements the existing set of functions with saturation mechanics.

cc #59983
@JohnTitor JohnTitor added B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC and removed C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Feb 28, 2020
@jhpratt
Copy link
Member

jhpratt commented May 1, 2020

What's holding up stabilization on this? It seems relatively simple.

@t-rapp
Copy link
Contributor Author

t-rapp commented May 5, 2020

In my opinion there is nothing specific holding this up from being stabilized. Initially I wanted to give it some time for others to comment, then forgot about. Added a PR for stabilization now.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue May 19, 2020
Stabilize saturating_abs and saturating_neg

Stabilizes the following signed integer functions with saturation mechanics:
 * saturating_abs()
 * saturating_neg()

Closes rust-lang#59983
@bors bors closed this as completed in f99344a May 19, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants