Json: Floating point imprecision

Created on 17 Jul 2018  路  3Comments  路  Source: nlohmann/json

Serializing floating point leads to imprecision.

Using this source code:

#include <iostream>

#include "json.hpp"

using json = nlohmann::json;

int main(void)
{
    json j;

    double a = 1.234;
    float b = 1.234;
    double c = b;
    float d = b;

    j["a"] = a;
    j["b"] = b;
    j["c"] = c;
    j["d"] = d;

    std::cout << std::setw(4) << j << std::endl;

    std::cout << "a=" << a <<std::endl;
    std::cout << "b=" << b <<std::endl;
    std::cout << "c=" << c <<std::endl;
    std::cout << "d=" << d <<std::endl;

    return 0;
}

Ouputs:

{
    "a": 1.234,
    "b": 1.2339999675750732,
    "c": 1.2339999675750732,
    "d": 1.2339999675750732
}
a=1.234
b=1.234
c=1.234
d=1.234

I was excepted every number to be equal to 1.234 as with the std::cout.

I am using g++ on Debian 9:

g++ (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
question invalid

Most helpful comment

Float is a 32-bit representation, and with 32 bits, the two numbers "1.234" and "1.2339999675750732" have identical representations. Either way is "correct" to print a float. There is no imprecision here, just a representation that is "less pretty".

When cout outputs "c" as "1.234", it is actually lying and printing something less precise than it actually is. If you tell cout you want full precision, then you will get identical results to what nlohmann::json is doing. (add "std::cout.precision(17);" to your program).

>>> def float2hex(num):
...  return ''.join('%02x' % ord(c) for c in struct.pack('!f', num))
...
>>> def double2hex(num):
...  return ''.join('%02x' % ord(c) for c in struct.pack('!d', num))
...
>>> float2hex(1.2339999675750732)
'3f9df3b6'
>>> float2hex(1.234)
'3f9df3b6'
>>>
>>> double2hex(1.234)
'3ff3be76c8b43958'
>>> double2hex(1.2339999675750732)
'3ff3be76c0000000'

All 3 comments

Float is a 32-bit representation, and with 32 bits, the two numbers "1.234" and "1.2339999675750732" have identical representations. Either way is "correct" to print a float. There is no imprecision here, just a representation that is "less pretty".

When cout outputs "c" as "1.234", it is actually lying and printing something less precise than it actually is. If you tell cout you want full precision, then you will get identical results to what nlohmann::json is doing. (add "std::cout.precision(17);" to your program).

>>> def float2hex(num):
...  return ''.join('%02x' % ord(c) for c in struct.pack('!f', num))
...
>>> def double2hex(num):
...  return ''.join('%02x' % ord(c) for c in struct.pack('!d', num))
...
>>> float2hex(1.2339999675750732)
'3f9df3b6'
>>> float2hex(1.234)
'3f9df3b6'
>>>
>>> double2hex(1.234)
'3ff3be76c8b43958'
>>> double2hex(1.2339999675750732)
'3ff3be76c0000000'

Wow very impressive response. Thanks for the explanation.

As I understood the background behind my _issue_, I close it.

@jaredgrubb Thanks so much for the detailed response. I am currently overworking the documentation, and this definitely should go in there!

Was this page helpful?
0 / 5 - 0 ratings