dev-cafe / parselglossy

Generic input parsing library, speaking in tongues.
https://parselglossy.readthedocs.io
MIT License
7 stars 2 forks source link

JSON and floating point precision #72

Open robertodr opened 5 years ago

robertodr commented 5 years ago

I was not aware of this, but apparently reading from/writing to JSON can mess up precision of floating point numbers. I don't really know how to test and prevent this...

bast commented 5 years ago

I will test this and once we have more data we can think about what we do with it.

bast commented 5 years ago

After a bit of reading (in particular https://docs.python.org/3/library/json.html#implementation-limitations), we can expect the serialized data to contain the same range and precision as the Python data types. But whether we lose the range and precision depends on who is reading the JSON.

If Python, my guess is that there is no significant loss. But with other JSON implementations, I guess it depends.

bast commented 5 years ago

I also tested this locally Python -> JSON -> Python. But I haven't tested reading into another language with another implementation.

robertodr commented 5 years ago

@stigrj can you run some tests in cpp-parselglossy? We need to check this also for the Fortran and C examples. If those JSON libraries do not present this problem, then I think we can document our preference for the JSON libraries for compiled languages and warning people about this possible pitfall with libraries we have not personally tested.

On a different note, I've never set a floating point parameter in an input file that employed the full range and precision... Any other input parser would hit the same problem, I think.

bast commented 5 years ago

I also think that this is not a JSON-specific problem but basically a problem of reading a number from file. We should check how C and Fortran read floats, testing both range and precision.

robertodr commented 5 years ago

Let's move this issue to the respective tutorial repositories?

On Thu, Apr 11, 2019 at 11:22 AM Radovan Bast notifications@github.com wrote:

I also think that this is not a JSON-specific problem but basically a problem of reading a number from file. We should check how C and Fortran read floats, testing both range and precision.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/dev-cafe/parselglossy/issues/72#issuecomment-482160046, or mute the thread https://github.com/notifications/unsubscribe-auth/ADiXEY2hicMLJxAqfQJinPWpVS9crjUyks5vf1M4gaJpZM4ciGWY .

-- Roberto Di Remigio

bast commented 5 years ago

Where will we document our findings? On the lib side or the tutorial side?

robertodr commented 5 years ago

A mention on both sides seems the safest bet.