News for hackers. Stuff that matters.
Re:One does not simply calculate the absolute value by nightcracker on Monday August 23, 2021 @01:41AM (#28272645)
This is why Rust has `abs`, `checked_abs`, `wrapping_abs`, `overflowing_abs`, `saturating_abs` and `unsigned_abs`.

Checked returns a Result enum that is an error on overflow, wrapping silently wraps (as in your answer), overflowing wraps but has a boolean indicator whether a wrap has occurred, saturating bounds at the types limits rather than wrapping and unsigned abs always gives the correct result albeit as a different type.

The unqualified abs panics on overflow on debug but silently wraps on release mode, like all Rust arithmetic code does by default.

There are no comments on this comment yet.