-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Default to printing floats with decimal instead of scientific notation #22971
base: master
Are you sure you want to change the base?
Conversation
Obviously formatting 1 as Many other programming languages and shells seem to default to using decimal notation when the number is within a relatively small "human friendly" range and scientific notation otherwise. E.g. Python seems to prefer decimal for values |
It shouldn't require 326 characters, I think, if it uses a smart algorithm like Dragonbox. |
No, the worst case decimal outputs are going to require a lot of characters regardless of algorithm, as they are generally aiming to have round-trippable output. |
Dragonbox has these three properties: https://github.com/jk-jeon/dragonbox?tab=readme-ov-file#introduction
It is quite worth implementing here instead of decimal output. |
Please read more about the algorithm you are suggesting. You are failing to understand what it is providing and what this MR is attempting to improve. Dragonbox is similar to Ryu (which zig implements) in that they are based on generating a signficand and exponent in shortest-form. The first sentence in your link explicitly specifies its purpose. Happy to talk about this elsewhere but this is not related to this MR. |
I re-read the PR, and I see what you mean. Separately, Dragonbox is objectively better than Ryu. We switched to it and have been quite happy in the Factor programming language. |
It's extremely easy to evaluate these things objectively. There's no reason to assert such things without evidence. Please don't assert performance claims without pointing to some reproducible benchmark. It's just noise on the issue tracker. Data points or gtfo |
Sorry, I'm not asserting performance claims. I'm making an assertion around the satisfying human-readability and correctness of the algorithm. It is also pretty fast. |
@tiehuis let me know if you have any thoughts for or against merging this--I'm interested in your take since you provided the ryu implementation. |
you might have missed #22971 (comment) |
Ah that's a good point--I'll look into how other languages decide what the cutoff is here. |
Currently,
std.fmt
defaults to formatting floats with scientific notation. IIUC this was originally due to some limitations in the float formatting code that have since been resolved.This results in some silly output, such as
@as(f32, 1)
being formatted as1e0
. Outside of being a bit odd, it makes it easy to misinterpret output if you miss thee
at the end of a number. You can just passd
to the formatter when formatting a single number, but this doesn't work if you're formatting e.g. a struct that contains fields with numbers. As such it's worth it to have a good default here.This PR changes the default to decimal, and updates the corresponding tests.