Skip to content

Commit

Permalink
Fix FloatOrd hash being different for different NaN values (#618)
Browse files Browse the repository at this point in the history
* 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 Oct 3, 2020
1 parent 22a2c88 commit 9a4167e
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 9a4167e

Please sign in to comment.