Skip to content

Loss of precision when serializing <double> #360

Closed

Description

@matspetter

It seems that precision is lost when serializing a double. I cannot say why since
std::numeric_limits::digits10 should provide enough digits !?
but if I change that to:
std::numeric_limits::digits10 then I'm not loosing anything.
I'm NOT using and "long double" types. Only "double".

Activity

  1. nlohmann commented on Nov 17, 2016

    @nlohmann
    Owner

    Do you have an example to check?

  2. gregmarr commented on Nov 17, 2016

    @gregmarr
    Contributor

    digits10
    number of decimal digits that can be represented without change
    that is, any number with this many decimal digits can be converted to a value of type T and back to decimal form, without change due to rounding or overflow.

    max_digits10
    number of decimal digits necessary to differentiate all values of this type
    that is, the number of base-10 digits that are necessary to uniquely represent all distinct values of the type T, such as necessary for serialization/deserialization to text.

  3. matspetter commented on Nov 17, 2016

    @matspetter
    Author

    Ok lets see. This code was run on a Mac.
    g++ --version

    Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/c++/4.2.1
    Apple LLVM version 8.0.0 (clang-800.0.42.1)
    Target: x86_64-apple-darwin16.1.0
    
    #include <string>
    #include <sstream>
    #include <iostream>
    #include <limits>
    
    int main(int argc,char** argv)
    {
        double v = 100000000000.1236;
    
        int p1 = std::numeric_limits<double>::digits10;
        int p2 = std::numeric_limits<double>::max_digits10;
    
        // stream with precision == digits10
        std::stringstream ss;
        ss.precision(p1);
        ss << v;
        std::cout << "digits10     " << p1 << ": " << ss.str() << std::endl;
    
        // stream with precision == max_digits10
        std::stringstream ss2;
        ss2.precision(p2);
        ss2 << v;
        std::cout  << "max_digits10 " << p2 << ": " << ss2.str() << std::endl;
    
        // Read back and compare with original
        double v1,v2;
        ss >> v1;
        ss2 >> v2;
    
        std::cout << "v==v1 : " << ((v==v1)?"true":"false") << std::endl;
        std::cout << "v==v2 : " << ((v==v2)?"true":"false") << std::endl;
    }

    output:

    digits10     15: 100000000000.124
    max_digits10 17: 100000000000.1236
    v==v1 : false
    v==v2 : true
    

    It is not easy with floating points and precision but this tells me that the streaming seems more correct when using "max_digits10"

  4. matspetter commented on Nov 18, 2016

    @matspetter
    Author

    It also gives the same result/output on a Ubuntu system:
    g++ --version
    g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609

    uname -a
    Linux mbergg12-ubuntu15 4.4.0-47-generic #68-Ubuntu SMP Wed Oct 26 19:39:52 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

  5. TurpentineDistillery commented on Nov 19, 2016

    @TurpentineDistillery

    This is not a bug, but a reality of dealing with floating-point representation.
    If one uses more than digits10 digits of precision, then string->value->string is not guaranteed to round-trip.

  6. gregmarr commented on Nov 19, 2016

    @gregmarr
    Contributor

    @TurpentineDistillery However using max_digits10 on output guarantees that value->string->value will not change. The difference being that not all strings of length max_digits10 can be generated by value->string with max_digits10 precision. So as long as string was generated by outputting a value, it's safe. The issue comes when the string was just an arbitrary string of digits.

  7. nlohmann commented on Nov 24, 2016

    @nlohmann
    Owner

    This issue can be closed, right?

  8. matspetter commented on Nov 24, 2016

    @matspetter
    Author
  9. added
    solution: invalid
    on Nov 24, 2016
  10. nlohmann commented on Nov 24, 2016

    @nlohmann
    Owner

    Thanks for the quick response!

  11. timueller commented on Jul 28, 2017

    @timueller

    I don't see how this is not a bug. The OP's example exactly illustrates the problem of value -> string -> value serialization/deserialization. We need to store double precision data in a json string form and read it back without loss of precision and have run into exactly the same problem.

  12. nlohmann commented on Jul 28, 2017

    @nlohmann
    Owner

    What would you propose?

  13. timueller commented on Jul 31, 2017

    @timueller

    Is there a problem if you just switch to using max_digits10 for both dump() and operator<< ?

  14. nlohmann commented on Jul 31, 2017

    @nlohmann
    Owner

    Then numbers like 2312.42 would be round-tripped to 2312.4200000000001.

  15. 24 remaining items

  16. nlohmann commented on Jan 27, 2018

    @nlohmann
    Owner

    The example from #360 (comment) works now and outputs 1.7976931348623157e+308.

  17. nlohmann commented on Jan 27, 2018

    @nlohmann
    Owner

    The roundtrips from #360 (comment) work.

  18. nlohmann commented on Jan 27, 2018

    @nlohmann
    Owner

    Roundtripping 2312.42 (#360 (comment)) works now.

  19. nlohmann commented on Jan 27, 2018

    @nlohmann
    Owner

    Roundtripping 100000000000.1236 (#360 (comment)) works now.

  20. nlohmann commented on Jan 27, 2018

    @nlohmann
    Owner

    Thanks to #915 from @abolz, this issue is now fixed. Thanks everybody for the patience!

  21. added
    solution: proposed fix
    and removed
    state: please discuss
    on Jan 27, 2018
  22. self-assigned this
    on Jan 27, 2018
  23. added this to the Release 3.1.0 milestone on Jan 27, 2018
  24. nlohmann commented on Nov 14, 2018

    @nlohmann
    Owner

    The library stores floating point numbers as double by default (you can change this in a template parameter).

    The number 21898.99 will be stored as the double 21898.990000000002. This is the name number the parser comes up to after reading the string 21898.99. You can check in your debugger that both numbers are equal down to the bit.

    image

    https://www.exploringbinary.com/floating-point-converter/

  25. charpty commented on Aug 9, 2021

    @charpty

    I learnt something today too, thanks for sharing

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

Metadata

Metadata

Assignees

Labels

solution: proposed fix

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions