Skip to content

Commit

Permalink
Fix FloatOrd hash being different for different NaN values (bevyengin…
Browse files Browse the repository at this point in the history
…e#618)

* Fix FloatOrd hash being different for different NaN values

* Fix FloatOrd hashing +0.0 and -0.0 to different values
  • Loading branch information
andrewhickman authored and mrk-its committed Oct 6, 2020
1 parent 2dad20d commit dafca80
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion crates/bevy_core/src/float_ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ impl Eq for FloatOrd {}

impl Hash for FloatOrd {
fn hash<H: Hasher>(&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());
}
}
}

Expand Down

0 comments on commit dafca80

Please sign in to comment.