I'm trying to tie together two pieces of software: one that gives me a f32
, and one that expects f64
values. In my code, I use f64::from(my_f32)
, but in my test, I compare the outcome and the value that I'm comparing has not been converted as expected: the f64
value has a bunch of extra, more precise, digits, such that the values aren't equal.
In my case, the value is 0.23
. Is there a way to convert the 0.23_f32
to f64
such that I end up with 0.23_f64
instead of 0.23000000417232513
?
fn main() {
let x = 0.23_f32;
println!("{}", x);
println!("{}", f64::from(x));
println!("---");
let x = 0.23_f64;
println!("{}", x);
println!("{}", f64::from(x));
}
Edit: I understand that floating-point numbers are stored differently--in fact, I use this handy visualizer on occasion to view the differences in representations between 32-bit and 64-bit floats. I was looking to see if there's some clever way to get around this.
Edit 2: A "clever" example that I just conjured up would be my_32.to_string().parse::<f64>()
--that gets me 0.23_f64
, but (obviously) requires string parsing. I'd like to think there might be something at least slightly more numbers-related (for lack of a better term).