Skip to content

Add saturating_abs and saturating_neg #59983

Closed

Description

@t-rapp

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

  1. 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.

  2. 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);
    
  3. 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.

  4. 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.

  5. added a commit that references this issue on Apr 25, 2019
  6. 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
  7. jhpratt commented on May 1, 2020

    @jhpratt
    Member

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

  8. 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.

  9. added a commit that references this issue on May 19, 2020
    4c48f5a
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-api[DEPRECATED; DO NOT USE]

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions