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

General floating point formatting in Debug with {:g?} #2729

Open
wants to merge 3 commits into
base: master
from

Conversation

@ExpHP
Copy link

commented Jul 21, 2019

Rendered

Proposes extending {:?} with a general number floating point flag g, similar to {:x?}:

assert_eq!(
    format!("{:g?}", vec![1e2, 1e4, 1e6, 1e8]),
    "[100.0, 10000.0, 1e6, 1e8]",
);

@ExpHP ExpHP changed the title General floating point formatting in `Debug` with `{:g?}`: General floating point formatting in 'Debug' with '{:g?}': Jul 21, 2019

@ExpHP ExpHP changed the title General floating point formatting in 'Debug' with '{:g?}': General floating point formatting in Debug with {:g?}: Jul 21, 2019

@ExpHP ExpHP changed the title General floating point formatting in Debug with {:g?}: General floating point formatting in Debug with {:g?} Jul 21, 2019

@ExpHP

This comment has been minimized.

Copy link
Author

commented Jul 31, 2019

Note: If people were on board with the much simpler alternative of simply changing the output of {:?} (which I honestly regard as broken), I'd be 100% behind that as well. I mean, strictly speaking, users aren't supposed to be relying on the output of Debug...

(though one major question is how to deal with the precision flag, since changing it to count sig figs would change the meaning of every line of code that uses Debug with a precision; perhaps adding a precision always switches to fixed-point)

@zpgaal

This comment has been minimized.

Copy link

commented Aug 2, 2019

Just braimstorming. As the format invocation and the format string parsing is generated by the compiler, what if instead of providing this single char (radix?) formating, let the user provide some generic trait impl for this functionality:

To format numbers as hex.
'''
format!("{:?Hex}", 12u32);
impl Formatter for Hex {
... //Something simmilar to Debug::fmt
}
'''
If there no "specialization" for a type, it emits error or use the default formatter. Also the question remains what about structures. If the formatter applies to the structure or to the members (primitive types like integers) only.

I don't know if it is possible to implement. It is just an idea that makes formting costomizable by users.

@ExpHP

This comment has been minimized.

Copy link
Author

commented Aug 3, 2019

Issue where the current behavior of Display actually creates confusion, because it implies that digits are zero when they are, in fact, not:

rust-lang/rust#63171

@rkruppe

This comment has been minimized.

Copy link
Member

commented Aug 3, 2019

@ExpHP

Note: If people were on board with the much simpler alternative of simply changing the output of {:?} (which I honestly regard as broken), I'd be 100% behind that as well. I mean, strictly speaking, users aren't supposed to be relying on the output of Debug...

This was attempted way back in rust-lang/rust#24612 but unfortunately this format change broke (and likely still breaks) real code, see rust-lang/rust#24612 (comment)

@ExpHP

This comment has been minimized.

Copy link
Author

commented Aug 3, 2019

Notice that the snippet in rust-lang/rust#24612 (comment) uses Display rather than Debug. My hope is that a change to Debug should not break any legitimate code (and a lot of the "illegitimate" code can be fixed with a crater run).

@rkruppe

This comment has been minimized.

Copy link
Member

commented Aug 3, 2019

Notice that the snippet in rust-lang/rust#24612 (comment) uses Display rather than Debug.

This is true but I don't think it's particularly relevant. The two snippets there are examples, not exhaustive: the specific library they're from has become largely irrelevant in the meantime, but the pattern (ensuring .0 is present and doing it in a way that would break if scientific notation is emitted) seems timeless and just as likely to occur with Debug as with Display.

My hope is that a change to Debug should not break any legitimate code (and a lot of the "illegitimate" code can be fixed with a crater run).

I do not think it is useful to try to draw a distinction between "legitimate" and "illegitimate" here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.