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
This commit is contained in:
Andrew Hickman 2020-10-03 20:56:25 +01:00 committed by GitHub
parent 22a2c88a47
commit 9a4167ef7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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());
}
}
}