-
Notifications
You must be signed in to change notification settings - Fork 813
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
json_dumps real value 7.6 as 7.59....96 #673
Comments
This is probably due to the fact to 7.6 not being presentable as a float in binary. https://en.wikipedia.org/wiki/Floating-point_arithmetic You can use the call JSON_REAL_PRECISION(). Thereby you force the internal use of the printf() family to round the number and that will "hide" this effect. I have the same issue. Doesn't everybody when placing prices in json with c-code? (Normaly you could also consider sending a price multiplied with a 100 so you send the prices as cents. Using JSON_REAL_PRECISION() had the disadvantage that it is for all "reals"; so they are all rounded the same way and that is not always desirable. I've tried to make a change/extension for this in the code. To the real object I added a precision. So with each real stored in json_t you can use a function that also allows you to set the precision for that specific real number. I've still to test it. But maybe I can get it accepted here as a change |
@wqboth you're right :) I hope you'll forgive my ignorance on this one. I had heard about this issue, but had never understood it. I can close this now. Edit: Oops, saw you added a change. Re-opening. |
I only did some spelling checks. I'm not a native speaker of English. So sometimes some corrections need to be made. |
But I have the same issue as you. It already frustrates me for quite some time. |
Do you need to be able to format floats with a specific precision, or do you just want to get rid of the non-optimal formatting of floats? My guess is that the former is very rarely useful. However, Jansson supports it because it has made it possible to fix the latter in some situations. For the latter use case, it would make more sense to incorporate something like https://github.com/ulfjack/ryu in Jansson to always format floats correctly. |
I will read the information in the url you just gave. My suggestion: |
https://github.com/ulfjack/ryu does have somenthing in common. To put it accurately there are two issues. One is the fact of numbers not being presentable and wanting to have more control over the precision. The fun thing is that when you have control over the precision you "hide"/"solve" the other issue implicitly. |
I've opened #680 that changes encoding of reals to output optimal values, i.e. no 7.599999... anymore. There's still a bit of work to do with the tests, but you can already check if it fixes your use case. |
This does indeed work for my use case:
Thank you. |
Behavior:
Steps to reproduce:
--prefix="/my/current/path"
I first noticed this with the jansson lib installed in ubuntu via
apt
, so this should exist in 2.13, as well.The text was updated successfully, but these errors were encountered: