Comments (9)
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.
But when using this in a API then the other party has to agree to conform to that method which is not always likely.)
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
from jansson.
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.
from jansson.
@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.
from jansson.
I only did some spelling checks. I'm not a native speaker of English. So sometimes some corrections need to be made.
from jansson.
But I have the same issue as you. It already frustrates me for quite some time.
Couple of years now. But i never got around to try to extend the possibilties of this otherwise great library.
from jansson.
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.
from jansson.
I will read the information in the url you just gave.
But indeed I do need a very specific formatting for floats. For example the numbers for prices may demand a different precision(variable number of decimals) than weights or other numbers.
This also has the advantage that you have control over binary non presentable numbers like 0.15. Because of the implicit rounding it will nicely be presented as 0.15 and not 0.1499999999....
That is why I created a branche of jansson with a proposition to a solution as you might have seen?
My suggestion:
https://github.com/wqboth/jansson/tree/real_precision_option
from jansson.
https://github.com/ulfjack/ryu does have somenthing in common.
I can see that this could be a used in printf()?
Or a replacement of printf() in jansson?
But even than I would like to have control over the precision per individual float value.
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.
from jansson.
This does indeed work for my use case:
$ ./a.out
7.6
Thank you.
from jansson.
Related Issues (20)
- Regarding Migration of Jansson 2.7 to 2.14
- json_dumps can only convert objects HOT 1
- How to use jansson with non-blocking I/O? HOT 1
- thread safety of json_incref and json_decref
- Can't create DLL on Windows for Jansson 1.2
- Cannot compile Jansson (make check fail) HOT 1
- "jansson_config.h" missing and cannot include the library in cmakelist HOT 2
- Create SECURITY.md HOT 1
- Error OOM HOT 2
- dump_string func can error return HOT 1
- json_int_t should be typedef'd as intmax_t
- Replacing values in a arbitrary JSON file HOT 1
- Target settings do not automatically populate when using CMake FetchContent HOT 1
- janssonConfig.cmake only packaged on Fedora and OpenMandriva HOT 2
- [question] multiple usage of json_set_alloc_funcs HOT 1
- Keep GitHub Actions up-to-date with dependabot
- conversion from JSON to string is not multithread-safe HOT 3
- conversion from string to JSON is not multithread-safe HOT 5
- json_unpack bug (jansson 2.13.1)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jansson.