From dafca8032fbdce816686f62b255c058e83ceb659 Mon Sep 17 00:00:00 2001 From: Andrew Hickman Date: Sat, 3 Oct 2020 20:56:25 +0100 Subject: [PATCH] Fix FloatOrd hash being different for different NaN values (#618) * Fix FloatOrd hash being different for different NaN values * Fix FloatOrd hashing +0.0 and -0.0 to different values --- crates/bevy_core/src/float_ord.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/bevy_core/src/float_ord.rs b/crates/bevy_core/src/float_ord.rs index 74496b77568db..2958e83857630 100644 --- a/crates/bevy_core/src/float_ord.rs +++ b/crates/bevy_core/src/float_ord.rs @@ -40,7 +40,15 @@ impl Eq for FloatOrd {} impl Hash for FloatOrd { fn hash(&self, state: &mut H) { - state.write(self.0.as_bytes()); + if self.0.is_nan() { + // Ensure all NaN representations hash to the same value + state.write(f32::NAN.as_bytes()) + } else if self.0 == 0.0 { + // Ensure both zeroes hash to the same value + state.write(0.0f32.as_bytes()) + } else { + state.write(self.0.as_bytes()); + } } }