Skip to content

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

Closed
seanmonstar opened this issue Feb 15, 2015 · 8 comments
Closed

0.9f32 != "0.9".parse().unwrap() #22349

seanmonstar opened this issue Feb 15, 2015 · 8 comments
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.

Comments

@seanmonstar
Copy link
Contributor

fn main() {
    assert_eq!(0.9f32, "0.9".parse().unwrap());
}
@edwardw
Copy link
Contributor

edwardw commented Feb 15, 2015

Hmm, it has to do with default numeric types, 0.9f64 works:

fn main() {
    assert_eq!(0.9f64, "0.9".parse().unwrap());
}

@shepmaster
Copy link
Member

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:

3f666666
3f666667

If you do the same for f64, the output is 3feccccccccccccd for both.

@shepmaster
Copy link
Member

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)
}

@seanmonstar
Copy link
Contributor Author

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.

@Diggsey
Copy link
Contributor

Diggsey commented Feb 15, 2015

Both parsers should give the representable float closest to the exact value.

@nagisa
Copy link
Member

nagisa commented Feb 15, 2015

However, this means there's 2 parsing implementations. 1 the compiler uses, and 1 the stdlib uses, and they differ somehow.

The compiler uses LLVM to parse literals and we can’t quite depend on LLVM for our standard library.

@steveklabnik steveklabnik added the A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. label Feb 15, 2015
@steveklabnik
Copy link
Member

Is this a dup of #7648 ?

@seanmonstar
Copy link
Contributor Author

@steveklabnik seems like it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.
Projects
None yet
Development

No branches or pull requests

6 participants