-
Notifications
You must be signed in to change notification settings - Fork 98
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
Improve control over the output format #27
Comments
Note that Ryu is inherently incapable of emitting arbitrary precision. You could modify the algorithm to emit additional digits in scientific or fixed notation, but after running out of the digits that are currently trimmed to achieve the shortest round-trip representation, you'd have to fill with zeroes. While it would still round-trip, it would be mathematically incorrect. Consider this example:
Ryu emits As part of implementing C++17 , I'll need to modify Ryu to emit shortest round-trip in fixed notation, but I am not yet sure how to contribute that upstream (I need to implement two more variants that switch between fixed and scientific depending on either printf's rules or an overall shortest criterion, so I need to figure out what the interface will be). I might end up separating the algorithm into two parts - the core part that generates the digits in a uint32_t/uint64_t and the exponent, and then a formatting part, which should allow all four charconv formats to be cleanly implemented. |
That is awesome. Finally hope for efficient string conversions in the standard. Edit: Although the wording is not extremely precise, it appears to imply to print more digits than significant digits available in the source floating point value.
|
Additional output formats would definitely be welcome, with the mentioned caveat. |
Marking this as specific to the C implementation. If someone is interested in special output formats for Java, please file a separate issue. |
I have code for fixed notation and I should have time to polish it up and submit a pull request in October. |
Here's the fixed notation code that I wrote for VS 2017 15.9 (slightly revised): https://github.com/StephanTLavavej/ryu/blob/msvc-2018.10.22/ryu/d2s.c#L388 It isn't ready for a pull request yet. Outstanding issues:
If this is interesting, I can continue to work on it after dealing with other things on my plate. |
I'm definitely interested in seeing some progress here. Unfortunately, I haven't had any time to work on this. |
Just to fix a dead link: https://github.com/StephanTLavavej/ryu/blob/bb357f7/ryu/d2s.c#L317. See https://reviews.llvm.org/D70631 for Steph's LLVM PR based on it (but updated with ryu printf); the PR implements Personally I would like to see %g become a thing too. |
Would it be possible to control whether the number is printed in exponent or decimal notation? Or, if more convenient, return the chosen location for the decimal point, if present, the number of digits as well as the exponent. And make it possible to get the chosen exponent as integer.
I'm trying to integrate this into a formatting library and as such would like to transform the output into all of the forms available for
printf()
. In particular, the decimal formatting with user provided precision makes it necessary to reformat the number heavily. While some are trivial to implement, reparsing the exponent to decide on the automatic formatting and moving the floating point separator feels suboptimal.The
double-conversion
interfaces could be a guideline here but the defaults and best options are quite specialized to ECMAScript.The text was updated successfully, but these errors were encountered: