diff --git a/crates/emath/src/ordered_float.rs b/crates/emath/src/ordered_float.rs index dd58a22a277..369541fed0d 100644 --- a/crates/emath/src/ordered_float.rs +++ b/crates/emath/src/ordered_float.rs @@ -124,13 +124,15 @@ mod private { #[inline] fn hash(&self, state: &mut H) { - if *self == 0.0 { - state.write_u8(0); - } else if self.is_nan() { - state.write_u8(1); + let bits = if self.is_nan() { + // "Canonical" NaN. + 0x7fc00000 } else { - self.to_bits().hash(state); - } + // A trick taken from the `ordered-float` crate: -0.0 + 0.0 == +0.0. + // https://github.com/reem/rust-ordered-float/blob/1841f0541ea0e56779cbac03de2705149e020675/src/lib.rs#L2178-L2181 + (self + 0.0).to_bits() + }; + bits.hash(state); } } @@ -142,13 +144,13 @@ mod private { #[inline] fn hash(&self, state: &mut H) { - if *self == 0.0 { - state.write_u8(0); - } else if self.is_nan() { - state.write_u8(1); + let bits = if self.is_nan() { + // "Canonical" NaN. + 0x7ff8000000000000 } else { - self.to_bits().hash(state); - } + (self + 0.0).to_bits() + }; + bits.hash(state); } } }