Reputation: 113
let s = format!("{}", t);
let x = s.parse::<f64>();
If t
is of type f64
, is there any case that the parsing back to f64
would fail?
Upvotes: 3
Views: 2912
Reputation: 15135
Doesn't seem so. This program covers all interesting edge cases, e.g. NaN, Infinity, Negative Infinity, etc, and it compiles and runs fine without panicking:
fn check_serialization_eq(nums: &[f64]) {
for num in nums {
let s = num.to_string();
let x = s.parse::<f64>().unwrap();
dbg!(num, x);
}
}
fn main() {
check_serialization_eq(&[
f64::MIN,
f64::MAX,
f64::NAN,
f64::INFINITY,
f64::NEG_INFINITY,
f64::EPSILON,
]);
}
Upvotes: 3