-
-
Notifications
You must be signed in to change notification settings - Fork 706
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
std.format: Add formatting integers with %e, %f, %g and %a. #8015
Conversation
|
Thanks for your pull request and interest in making D better, @berni44! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla references
Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + phobos#8015" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to add @thewilsonator to this list. He did a lot of similar reviews in the past. :-) |
I would say that the appropriate mental model here is that requesting to format an integral type as if it were floating point performs implicit conversion to floating point (as occurs for functions or variables), so the behavior should match. |
I agree on this mental model. I used it for everything (aside from defaults). I even wrote an external test comparing the output to the output of a cast, to make sure I didn't miss anything. So, when I read your post, I wondered, why I disagree in case of defaults. Meanwhile I think, it's because in my opinion the defaults of floats are not chosen well and they are just kept for backward compatibility. (See issue 17715 where someone complains about these defaults - it could have been me. Or An introduction to floating point numbers, something I wrote some weeks ago - I don't remember why - but it shows, how the current defaults cause problems.) All in all, I think, that my gut feeling just tells me, that it would be a good thing to avoid transfering these problems from floats to integers. |
I'm not a fan of them either. |
Inspired by issue 16078 I added formatting with
%eetc. for integers. I tried to reach an identical result to first casting and then formatting, while preserving all digits:yields:
As can be seen: With the cast, the last four digits are lost while they are preserved without.
I decided to deviate from the floating point behavior in two places:
%a, the integral digit can be larger than 1.%atries to resemble the mantissa and therefore the integral digit is always 0 or 1 and the fractional digits match with the fractional digits of the mantissa. Integral values don't have a mantissa nor have they fractional digits. So it's more natural to build them from right to left instead of left to right which is natural for floating point values. But that leads to larger integral digits. (And as a side effect, the digits of%aand%xare the same which is also preferable.)The first commit adds a rounding tool. I plan to use this to simplify the functions in
std.format.internal.floats. Therefore I put it into a separate function.