-
Notifications
You must be signed in to change notification settings - Fork 13.3k
0.9f32 != "0.9".parse().unwrap() #22349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hmm, it has to do with default numeric types, fn main() {
assert_eq!(0.9f64, "0.9".parse().unwrap());
} |
Is this not just normal floating point wonkyness? Here's what happens if you print a hex representation of the values: use std::mem::transmute;
fn main() {
let lit: f32 = 0.9;
let prs: f32 = "0.9".parse().unwrap();
let lit_int: u32 = unsafe{transmute(lit)};
let prs_int: u32 = unsafe{transmute(prs)};
println!("{:x}", lit_int);
println!("{:x}", prs_int);
} outputs:
If you do the same for |
Another example of (expected) floating point excitement: fn main() {
let lit: f32 = 0.9;
let sum: f32 = 0.3 + 0.3 + 0.3;
assert_eq!(lit, sum)
} |
Yea, floats are wonky. However, this means there's 2 parsing implementations. 1 the compiler uses, and 1 the stdlib uses, and they differ somehow. |
Both parsers should give the representable float closest to the exact value. |
The compiler uses LLVM to parse literals and we can’t quite depend on LLVM for our standard library. |
Is this a dup of #7648 ? |
@steveklabnik seems like it! |
The text was updated successfully, but these errors were encountered: