-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Inexact results from str::float::from_str #7648
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
As jensnockert points out on IRC, |
It should be exactly equal, since 670.0 is exactly representable in IEEE 754. #7030 is probably the same issue. Ps. Even with a float RHS, Python gives the correct answer
|
This line is responsible, I think. |
No idea, parsing floats is non-trivial. considering Java, PHP &c. have had severe DOS issues related to parsing floats, I think we should just borrow known-good code. |
Yeah the current implementation just uses an very naive approach for float parsing and emitting. The problem is that doing it proper is both hard and slow. To get an optimum result for parsing/printing a float from/to a base-n string representation requires bignum fractionals. (where optimum == "shortest unambigious string representation in base n" in a roundtrip float -> string -> float) There exist algorithms specifically tailored to base 10 that can get around that by only requiring bignum ints, or even regular fixed sized integers, and working with bases 2, 4, 8, 16 and 32 is trivial, so I guess we'd ultimately want to incorporate all those techniqes by having different code paths to take, depending on input and base. |
visiting for bug triage, email from 2013-08-26. It would be nice to fix this. I haven't looked at the rust implementation in
And yes, I think these algorithms are specially tailored to base 10, as Kimundi noted. |
Triage. This is still a problem. The papers pnkfelix links look really nice. |
cc #6220 |
This needs to be properly triaged. Nominating (I suspect it will be flagged as merely P-high, as that was what was done with #7030). |
Assigning P-high, not 1.0. |
Hello, From a discussion on the fn main() {
let a = from_str::<f32>("3.141592").unwrap();
let b = 3.141592f32;
println!("{}", a == b); // false
} So, the implementations of string parsing in the compiler and in Regards, |
Closing as a forward-dupe of #24557, since that issue has more info. |
Some inputs to
std::float::from_str
give a value that prints as an integer, but does not compare equal to it:For comparison, Python does not have this issue:
The text was updated successfully, but these errors were encountered: