3 ms·
I guess because floats implement PartialOrd, not Ord, and what would you return if one of self, min, and max is non-ordered? Returning an `Option<T>` would usua
by masklinn 6y ago
I guess because floats implement PartialOrd, not Ord, and what would you return if one of self, min, and max is non-ordered? Returning an `Option<T>` would usually be inconvenient.
Therefore clamp is implemented on Ord (meaning there's always a value to return), and there's an efficient implementation on floats which can define its behaviour with respect to NaN.
If you want the actual details,
* https://internals.rust-lang.org/t/clamp-function-for-primitive-types/4999 https://internals.rust-lang.org/t/clamp-function-for-primiti...
* https://github.com/rust-lang/rust/issues/44095 https://github.com/rust-lang/rust/issues/44095
- pornel 6y agoAlso the float implementation was chosen to optimize well with SSE, even in presence of NaNs.