Skip to content
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

uabs/unsigned_abs #2914

Closed
sollyucko opened this issue Apr 25, 2020 · 4 comments
Closed

uabs/unsigned_abs #2914

sollyucko opened this issue Apr 25, 2020 · 4 comments

Comments

@sollyucko
Copy link

It would be great to have a uabs or unsigned_abs (or something else, the name isn't important) method that returns the absolute value of the given signed integer as an unsigned integer of the same size. (e.g. uabs(self: i8) -> u8)

Advantages

  • It encodes the guarantee that the result is positive.
  • It is more concise and less error-prone than workarounds

Disadvantages

  • None?

Implementation

impl iX {
    pub fn uabs(self) -> uX {
        self.wrapping_abs() as uX
    }
}

Alternatives

  • x.abs() as uX
    • iX::MIN.abs() panics in debug mode
  • x.wrapping_abs() as uX
    • Very verbose
  • if x == iX::MIN { x as uX } else { x.abs() as uX }
    • Extremely verbose
  • Redefining iX::abs
    • May cause compatibility issues

Related: #1017, rust-lang/rust#2353

@sollyucko
Copy link
Author

sollyucko commented Apr 25, 2020

If x == i8::MIN == -128_i8, then x.wrapping_abs() == -128_i8 and then, since in 8-bit 2's complement, -128_i8 is represented as 0b1000_0000, when converted to u8, 0b1000_0000 is interpreted as 128_u8, which is correct.

@RustyYato
Copy link

@sollyucko That works playground

@RalfJung
Copy link
Member

Yes please! wrapping and as set off all the alarm bells when looking for bugs in integer code, so having a method that is just documented as "always returns the correct absolute value, no wrapping ever" would be great.

Manishearth added a commit to Manishearth/rust that referenced this issue Aug 3, 2020
add `unsigned_abs` to signed integers

Mentioned on rust-lang/rfcs#2914

This PR simply adds an `unsigned_abs` to signed integers function which returns the correct absolute value as a unsigned integer.
Manishearth added a commit to Manishearth/rust that referenced this issue Aug 3, 2020
add `unsigned_abs` to signed integers

Mentioned on rust-lang/rfcs#2914

This PR simply adds an `unsigned_abs` to signed integers function which returns the correct absolute value as a unsigned integer.
JohnTitor added a commit to JohnTitor/rust that referenced this issue Aug 3, 2020
add `unsigned_abs` to signed integers

Mentioned on rust-lang/rfcs#2914

This PR simply adds an `unsigned_abs` to signed integers function which returns the correct absolute value as a unsigned integer.
JohnTitor added a commit to JohnTitor/rust that referenced this issue Aug 4, 2020
add `unsigned_abs` to signed integers

Mentioned on rust-lang/rfcs#2914

This PR simply adds an `unsigned_abs` to signed integers function which returns the correct absolute value as a unsigned integer.
@scottmcm
Copy link
Member

scottmcm commented Sep 3, 2020

Looks like this went in: https://doc.rust-lang.org/nightly/std/primitive.i64.html#method.unsigned_abs

Anyone interested should follow the tracking issue now: rust-lang/rust#74913

@scottmcm scottmcm closed this as completed Sep 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants