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

println!("{}", 9.4 * 3.0) #125056

Closed
chedongzhe opened this issue May 13, 2024 · 4 comments
Closed

println!("{}", 9.4 * 3.0) #125056

chedongzhe opened this issue May 13, 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

@chedongzhe
Copy link

println!("{}", 9.4 * 3.0);
output: 28.200000000000003

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 13, 2024
@snowfoxsh
Copy link

That is the expected output. https://docs.python.org/3/tutorial/floatingpoint.html. This is just an issue with how floating point numbers in general work.
image

@LunarLambda
Copy link

Maybe you are looking for the precision field:

println!("{:.2}", 9.3 * 3.0);

will print 27.90.

@jieyouxu jieyouxu added C-discussion Category: Discussion or questions that doesn't represent real issues. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 13, 2024
@cuviper
Copy link
Member

cuviper commented May 14, 2024

Float Exposed is also a useful reference, as it will show you that the closest f64 (double) to 9.4 is 9.40000000000000035527 -- and even that is an approximation, but you can find the exact base-10 number later on the page. So you're starting with a higher input than you wanted, and then the multiplication result comes out higher than you wanted too.

On the other hand, 3.0 is exact.

@fmease fmease added the A-floating-point Area: Floating point numbers and arithmetic label May 14, 2024
@fmease
Copy link
Member

fmease commented May 14, 2024

Closing as works-as-intended.

@fmease fmease closed this as not planned Won't fix, can't repro, duplicate, stale May 14, 2024
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

7 participants