3 ms·
pub fn clamp(self, min: f64, max: f64) -> f64 Rust has generics iirc, so why there's this?
by coldtea 6y ago
pub fn clamp(self, min: f64, max: f64) -> f64
Rust has generics iirc, so why there's this?
- masklinn 6y agoI 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.