I have a unique scenario where I want to use f64 as a key in a HashMap. In particular I know the f64 will never be NaN and I can tolerate f64s that should be equal, but are not. So I transmute() the f64 to u64. However, when I pull the u64 out the HashMap and transmute() it back to a f64 it is a different value. Code below and on playground.
use std::collections::HashMap;
fn main() {
let x = 5.0;
let y: u64 = unsafe { std::mem::transmute(x) };
let x: f64 = unsafe { std::mem::transmute(y) };
println!("y: {}, x: {}", y, x);
let mut hash = HashMap::new();
hash.insert(y, 8);
for (y, _) in &hash {
let x: f64 = unsafe { std::mem::transmute(y) };
println!("y: {}, x: {}", y, x);
}
}
What am I missing?