-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Unexpected float value when casted from f32
to f64
#125777
Comments
This is not a bug. You can see the same behavior with slightly different rounding from C: #include <stdio.h>
int main() {
float f = 128.2;
printf("%f\n", (double)f);
return 0;
} will print
The lossy step is the conversion of 128.2 from base 10 to base 2. The base-2 representation has the equivalent of a repeating decimal. |
Not a bug
prints
This is just the printing algorithm selecting a shorter number of the many reals that map to to that float bit pattern |
If only we had hex and binary formatting for floats. |
Make it happen! You have the power 💪 |
I tried this code:
I expected to see this happen: value_64: 128.2
Instead, this happened: value_64: 128.1999969482422
Meta
rustc --version --verbose
:Backtrace
The text was updated successfully, but these errors were encountered: