Closed therealbnut closed 4 years ago
You could write something generic like that, although note that Ord
will exclude all floating point. For the function you wrote, you'd need where T: PartialOrd, for<'a> &'a T: Sub<&'b T, Output = T>
.
But for num-traits, that helper is specifically associated with Signed
, a common convention throughout the crate. It also doesn't work quite how you're expecting:
Returns zero if
x
is less than or equal toy
, otherwise the difference betweenx
andy
is returned.
This corresponds to fdim
, aligned with std
's deprecated inherent method of floats.
Deprecated since 1.10.0: you probably meant
(self - other).abs()
: this operation is(self - other).max(0.0)
except thatabs_sub
also propagates NaNs (also known asfdim
in C). If you truly need the positive difference, consider using that expression or the C functionfdim
, depending on how you wish to handle NaN (please consider filing an issue describing your use-case too).
We should probably just deprecate it in num-traits too... see also #120.
Oh cool, thanks for the historic context and clarification. I’m happy to close this and use my implementation as it works where I need it :)
Hey, I wanted to take the absolute difference between two numbers, but I couldn't with num_traits. It seems like you should be able to. Is there any reason why not?