From 7b70c9a860dd7598c8dc3c2524654c371da9aa09 Mon Sep 17 00:00:00 2001 From: Andrew Hickman Date: Fri, 2 Oct 2020 20:40:36 +0100 Subject: [PATCH 1/2] Fix FloatOrd hash being different for different NaN values --- crates/bevy_core/src/float_ord.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/bevy_core/src/float_ord.rs b/crates/bevy_core/src/float_ord.rs index 74496b77568db..a8048eec74842 100644 --- a/crates/bevy_core/src/float_ord.rs +++ b/crates/bevy_core/src/float_ord.rs @@ -40,7 +40,12 @@ 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 { + state.write(self.0.as_bytes()); + } } } From 46b5b7e7a4ab39317b1e160a5a8f745982b111a4 Mon Sep 17 00:00:00 2001 From: Andrew Hickman Date: Fri, 2 Oct 2020 20:59:37 +0100 Subject: [PATCH 2/2] Fix FloatOrd hashing +0.0 and -0.0 to different values --- crates/bevy_core/src/float_ord.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/bevy_core/src/float_ord.rs b/crates/bevy_core/src/float_ord.rs index a8048eec74842..2958e83857630 100644 --- a/crates/bevy_core/src/float_ord.rs +++ b/crates/bevy_core/src/float_ord.rs @@ -43,6 +43,9 @@ impl Hash for FloatOrd { 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()); }