-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Custom Precision on floating point numbers #1170
Comments
Workaround: #1191 |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
FWIW, I feel this feature would be great as well . The workaround is a bit ugly, and I am not sure it will always work. #1121 seems to have gone stale unfortunately 😞 |
Regarding the workaround, the old, prettier workaround described in #768 comments stopped working due to the new Grisu2 algorithm, which is selected on platforms that implement IEEE-754 floating points, i.e. almost all of them. The old workaround works if you disable the platform check, i.e. patch |
|
@dvjdjvu That only works if that number can be represented exactly by a |
why did this get closed? I would say this is a bug, since the library simply ignores the precision setting of the stream it is writing to. |
I really want this to work properly (we dont need 10 places after the dot and it wastes a lot of space) and will submit a pr later |
stale bot closed as "completed"? wow, now we complete issues by letting them go stale :-D. anyhow, i know this is open source and we didnt pay for this, so if we want this I will implement it, l8er :) |
I would be glad to see a PR |
I have something working. it is not 100% backward compatible though, since for dumping to an ostream it takes the precision parameter of the ostream as a precision, which defaults to 6. now it can probably be more. I personally think 6 is enough for any realistic use case, but your mileage may vary. if you don't want to accept that then i dont know how to parameterize it for ostream dumping. well, unless you want to set it on the json itself, or you could make some fancy wrapper i.e. |
I'm not one of developers here, but I think only 100% backwards compatible solutions will be accepted. |
so you think it is important to keep the 15 or so digits after the decimal points by default? Idk, I feel that very mostly it would be welcome if not so many digits are emitted. you could still do all the digits if you need by using setprecision. I think this would be a very justified "breakage", but ya, my decision. But if really wanted I can do the wrapper approach, where you would "annotate" the json while streaming. |
ok, ya, i can do easily: |
Yes, breaking changes are a no-go. That's the reason we have so many macros or other configuration options. |
So what can i push? The annotating wrapper and using stream precision as a switchable feature? |
Will go with that then and add a test, PR should be ready some time later today |
When storing floating point values, exact representation is not possible.
ie: -0.2 -> -0.019999995827674866
However, when precision is not a big issue this introduces huge about of bloat in the JSON file. So there should be an option to specify how many characters of precision. Like it is possible in std::ostringstream (std::ostreams), and in printf.
Currently these 2 methods would be desired:
The text was updated successfully, but these errors were encountered: