Git Product home page Git Product logo

Comments (9)

wqboth avatar wqboth commented on July 17, 2024 1

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.

akheron avatar akheron commented on July 17, 2024 1

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.

mrryanjohnston avatar mrryanjohnston commented on July 17, 2024

@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.

wqboth avatar wqboth commented on July 17, 2024

I only did some spelling checks. I'm not a native speaker of English. So sometimes some corrections need to be made.

from jansson.

wqboth avatar wqboth commented on July 17, 2024

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.

akheron avatar akheron commented on July 17, 2024

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.

wqboth avatar wqboth commented on July 17, 2024

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.

wqboth avatar wqboth commented on July 17, 2024

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.

mrryanjohnston avatar mrryanjohnston commented on July 17, 2024

This does indeed work for my use case:

$ ./a.out 
7.6

Thank you.

from jansson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.