You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.
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.
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.
Activity
hellow554 commentedon Apr 15, 2019
What do you expect them to return? Just
0
for the two values that can't be expressed? IMHO you should prefer thecheck_$opr
instead and explicitly handle that.t-rapp commentedon Apr 15, 2019
The
abs
andneg
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 existingsaturating_add/sub/mul/pow
functions.scottmcm commentedon Apr 16, 2019
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 thechecked_
approach.t-rapp commentedon 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.
Auto merge of #60192 - t-rapp:tr-saturating-funcs, r=alexcrichton
jhpratt commentedon May 1, 2020
What's holding up stabilization on this? It seems relatively simple.
t-rapp commentedon 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.
3 remaining items