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

"{:?}" for 0. is 0 instead of 0. #30967

Closed
yongqli opened this issue Jan 17, 2016 · 8 comments
Closed

"{:?}" for 0. is 0 instead of 0. #30967

yongqli opened this issue Jan 17, 2016 · 8 comments
Labels
C-feature-accepted Category: A feature request that has been accepted pending implementation. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@yongqli
Copy link

yongqli commented Jan 17, 2016

This code:

fn main() { 
    println!("{:?}", 0.);
}

produces 0, but I would expect 0.. This breaks the convention of Rust source code.

https://play.rust-lang.org/?gist=e1f1c703b2c582ddc3c3&version=nightly

@yongqli yongqli changed the title "{:?}" for 0. is wrong "{:?}" for 0. is 0 instead of 0. Jan 17, 2016
@jonas-schievink
Copy link
Contributor

There's no way to distinguish between 0f64, 0. or 0.0 or any other representation of 0 at runtime.

IIRC The current float pretty printer just uses as many decimal places needed to prevent rounding errors from reading it back. However, I don't think it's a bad idea to always print floats unambiguously (ie. always print a decimal point, even when not needed for accuracy).

@yongqli
Copy link
Author

yongqli commented Jan 17, 2016

Yes, I would like it to always print 0. so that I can read back the outputted value as valid rust source code.

@hanna-kruppe
Copy link
Contributor

This was discussed before, and IIRC rejected because of backwards compatibility concerns. Or maybe I'm mistaken and it was only using exponential notation by default that was rejected. See #24612 for some context, I'm sure there's more.

@hanna-kruppe
Copy link
Contributor

Also, do I understand correctly that your use case would require all floats that can be printed as integers to include a trailing .? I think Rust's Debug doesn't even try to follow the guideline (used for example by Python for repr(), and even there it's just a guideline) that the output should be valid to copy & paste into code. So if this is your sole motivation, I think it's misguided.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 17, 2016

just use {:.1} as a format specifier to guarantee at least one digit after the dot.

@bluss
Copy link
Member

bluss commented Jan 17, 2016

I'll go out on a limb and say that {:#} (alternate Display formatting) for floating point could always print the decimal point (Debug too?). It's only usable directly however; not reachable when the number is part of a struct.

This issue does of course relate to all whole number floating point values, not just zero.

@steveklabnik
Copy link
Member

/cc @rust-lang/libs

@steveklabnik steveklabnik added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed A-libs labels Mar 24, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jul 24, 2017
@dtolnay dtolnay added C-feature-accepted Category: A feature request that has been accepted pending implementation. and removed C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Nov 17, 2017
@dtolnay
Copy link
Member

dtolnay commented Nov 17, 2017

I agree that the Debug representation of a float should clearly look like a float. 0u64 and 0f64 should not have the same {:?}. My preference would be for 0.0 but I could live with 0..

Marking feature-accepted as I would be prepared to consider a PR fixing this, but if the PR turns out to be controversial then we may ask for an RFC.

kennytm added a commit to kennytm/rust that referenced this issue Dec 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-accepted Category: A feature request that has been accepted pending implementation. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

8 participants