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

Unexpected float value when casted from f32 to f64 #125777

Closed
comphead opened this issue May 30, 2024 · 4 comments
Closed

Unexpected float value when casted from f32 to f64 #125777

comphead opened this issue May 30, 2024 · 4 comments
Labels
A-floating-point Area: Floating point numbers and arithmetic C-discussion Category: Discussion or questions that doesn't represent real issues.

Comments

@comphead
Copy link

I tried this code:

    println!("value_64: {:?}", 128.2_f32 as f64);

I expected to see this happen: value_64: 128.2

Instead, this happened: value_64: 128.1999969482422

Meta

rustc --version --verbose:

Tested on 1.78 Stable, but can see this also on 1.80 nightly. Not sure about earlier versions
Backtrace

<backtrace>

@comphead comphead added the C-bug Category: This is a bug. label May 30, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 30, 2024
@saethlin saethlin added A-floating-point Area: Floating point numbers and arithmetic C-discussion Category: Discussion or questions that doesn't represent real issues. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 30, 2024
@saethlin
Copy link
Member

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

128.199997

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.

@saethlin saethlin closed this as not planned Won't fix, can't repro, duplicate, stale May 30, 2024
@the8472
Copy link
Member

the8472 commented May 30, 2024

Not a bug

    println!("value_32: {:.20?} bits: {:b}", 128.2_f32, 128.2_f32.to_bits());
    println!("value_64: {:.20?} bits: {:b}", 128.2_f32 as f64, (128.2_f32 as f64).to_bits());

prints

value_32: 128.19999694824218750000 bits: 1000011000000000011001100110011
value_64: 128.19999694824218750000 bits: 100000001100000000001100110011001100000000000000000000000000000

This is just the printing algorithm selecting a shorter number of the many reals that map to to that float bit pattern

@the8472
Copy link
Member

the8472 commented May 30, 2024

If only we had hex and binary formatting for floats.

@saethlin
Copy link
Member

saethlin commented May 30, 2024

Make it happen! You have the power 💪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-floating-point Area: Floating point numbers and arithmetic C-discussion Category: Discussion or questions that doesn't represent real issues.
Projects
None yet
Development

No branches or pull requests

4 participants