-
Notifications
You must be signed in to change notification settings - Fork 141
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
The documentation contradicts the meaning of abs_sub #120
Comments
FWIW, the behavior is like C's
Maybe we should just deprecate it (rust-num/num#365), as the corresponding method in |
I think we should deprecate it. I was not aware of |
Ideally we would deprecate it while making it not a required method for the trait anymore. There's not a great way to write it generically though, since we don't have |
My sketch can overflow, but that is essentially how it works for #[inline]
fn abs_sub(&self, other: &$t) -> $t {
if *self <= *other { 0 } else { *self - *other }
} |
The existing code can overflow for |
Okay, I see what you mean. So if I changed that sketch to be the same as the current implementation, like fn abs_sub(&self, other: &Self) -> Self {
self <= other {
Self::zero()
} else {
*self - *other
}
} Then you would be okay with deprecating it and providing a built-in implementation? |
That would be fine, but we can't use |
Anyway, having a default impl is ideal for deprecation, but not mandatory. We could still deprecate it as-is, just with the annoying fact that anyone implementing the trait still has to write this method. |
In order to (possibly) have a positive overflow, In order to (possibly) have a negative overflow, Alternately, since it is only required that That being said, |
When I hear
abs_sub
, I expect it to be|a-b|
. At the very least, I think that theabs_sub
function could be created as some composition or combination of theabs
function and thesub
function, but it appears thatabs_sub
is more of a saturating sub or a clamped sub (i.e.max(0, a-b)
)Furthermore, the documentation seems to contradict itself -- "The positive difference of two numbers" is not the same as "
zero
if the number is less than equal toother
". For example, witha=3
andb=5
, I would expect the positive difference to mean the differencea - b = 3 - 5 = -2
, but now positive= 2
.And finally, if a Type implements
Num
(which includesNumOps
, which includesSub
) and it implementsSigned
, then abs_sub will always have the same form ofI suggest either documenting
abs_sub
as meaning|a-b|
, and providing an implementation or removing it entirely.The text was updated successfully, but these errors were encountered: