Skip to content

Add saturating_abs and saturating_neg #59983

Closed
@t-rapp

Description

@t-rapp
Contributor

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.

Activity

hellow554

hellow554 commented on Apr 15, 2019

@hellow554
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.

added
C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
on Apr 15, 2019
t-rapp

t-rapp commented on Apr 15, 2019

@t-rapp
ContributorAuthor

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

scottmcm commented on Apr 16, 2019

@scottmcm
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

t-rapp commented on Apr 16, 2019

@t-rapp
ContributorAuthor

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

added a commit that references this issue on Apr 25, 2019

Auto merge of #60192 - t-rapp:tr-saturating-funcs, r=alexcrichton

added
B-unstableBlocker: Implemented in the nightly compiler and unstable.
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
and removed
C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
on Feb 28, 2020
jhpratt

jhpratt commented on May 1, 2020

@jhpratt
Member

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

t-rapp

t-rapp commented on May 5, 2020

@t-rapp
ContributorAuthor

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.

3 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @hellow554@jonas-schievink@jhpratt@scottmcm@t-rapp

      Issue actions

        Add saturating_abs and saturating_neg · Issue #59983 · rust-lang/rust