Skip to content

Floating point imprecision #1166

Closed
Closed

Description

@jmlemetayer

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

Activity

  1. jaredgrubb commented on Jul 17, 2018

    @jaredgrubb
    Contributor

    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'
    
  2. jmlemetayer commented on Jul 17, 2018

    @jmlemetayer
    Author

    Wow very impressive response. Thanks for the explanation.

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

  3. nlohmann commented on Jul 18, 2018

    @nlohmann
    Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions