Skip to content
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

float::from_str returns 0 for one space, and NaN for multiple spaces #1089

Closed
mbrubeck opened this issue Oct 28, 2011 · 5 comments
Closed

float::from_str returns 0 for one space, and NaN for multiple spaces #1089

mbrubeck opened this issue Oct 28, 2011 · 5 comments
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@mbrubeck
Copy link
Contributor

float::from_str(" ") (the input is a string of two space characters) returns NaN in Rust. It should return 0.0.

float::from_str(" ") (with just one space character) returns 0.0.

There is a test in stdtest/float.rs that should have caught this bug, but it is falsely passing because (NaN == 0.0) is true (#1083).

@brson
Copy link
Contributor

brson commented Oct 28, 2011

Are we sure that spaces should parse? uint::from_str doesn't look like it parses space (actually it looks like it might do something crazy).

@mbrubeck
Copy link
Contributor Author

Good question. For comparison, atof in C/C++, new Number(str) in JavaScript, and Double.valueOf(str) in Java all allow leading and trailing whitespace.

However, Rust's implementation is definitely not designed to do so, so it seems like an accident that it allows a single space in some cases. It should be changed to either allow or disallow any amount of leading and trailing space.

@brson
Copy link
Contributor

brson commented Oct 28, 2011

That sounds like good precedent to me. We should probably make sure they allow all whitespace characters then (as defined in char::is_whitespace)

@mbrubeck
Copy link
Contributor Author

Some more quirks:

float::from_str("e") yields zero, which I think is unintentional.

float::from_str(".") yields zero too, which is intentional and documented but seems like it might be a bad idea. . is not a valid floating-point literal, and any code that passes it to from_str seems likely to be a mistake.

mbrubeck added a commit to mbrubeck/rust that referenced this issue Oct 30, 2011
Discard leading and trailing whitespace, for consistency with C/JS/Java/etc.
Also, don't allow floating point numbers that start or end with 'e'.
brson pushed a commit that referenced this issue Oct 30, 2011
Discard leading and trailing whitespace, for consistency with C/JS/Java/etc.
Also, don't allow floating point numbers that start or end with 'e'.
@mbrubeck
Copy link
Contributor Author

Fixed by #1103

bors pushed a commit to rust-lang-ci/rust that referenced this issue Oct 26, 2020
Add custom rustc driver that uses cg_clif
coastalwhite pushed a commit to coastalwhite/rust that referenced this issue Aug 5, 2023
celinval added a commit to celinval/rust-dev that referenced this issue Jun 4, 2024
We were trying to figure out the values of those properties during runtime, which is not possible. Fix the code to look for the information in the vtable instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

2 participants