[Boost-bugs] [Boost C++ Libraries] #10639: lexical_cast<double>(string) wrong in C++11

Subject: [Boost-bugs] [Boost C++ Libraries] #10639: lexical_cast<double>(string) wrong in C++11
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2014-10-09 14:05:01


#10639: lexical_cast<double>(string) wrong in C++11
-------------------------------+--------------------------
 Reporter: richard.kreckel@… | Owner: apolukhin
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: lexical_cast
  Version: Boost 1.56.0 | Severity: Problem
 Keywords: |
-------------------------------+--------------------------
 It's a well-known fact that printing a double precision floating-point
 number to 17 decimal digits is enough to recover the original double
 precision number (c.f. Theorem 15 in David Goldberg's "What Every Computer
 Scientist Should Know About Floating-Point Arithmetic").

 This useful property has been broken somewhere between Boost 1.40 and
 Boost 1.55 if the program is compiled with -std=c++11 or -std=c++14 (GCC
 or CLang compiler switches).

 Here is a test program which should print the same number three times:
 {{{
 #include <iostream>
 #include <string>
 #include <boost/lexical_cast.hpp>
 #include <boost/math/special_functions/next.hpp>
 #include <boost/format.hpp>

 int main()
 {
     double x = 1.0316536643319239e-255;
     std::printf("x == %.17g\n", x);

     std::string s = (boost::format("%.17g") % x).str();
     std::printf("s == %s\n", s.c_str());

     double y = boost::lexical_cast<double>(s); // ERROR
     std::printf("y == %.17g\n", y);

     if (x != y) {
         std::cout << "x and y are "
                   << boost::math::float_distance(x, y)
                   << " ULP apart" << std::endl;
         return 1;
     }

     return 0;
 }
 }}}

 However, with Boost 1.55 or 1.56 it prints:
 {{{
 x == 1.0316536643319239e-255
 s == 1.0316536643319239e-255
 y == 1.031653664331924e-255
 x and y are 1 ULP apart
 }}}

 I'm testing this on an AMD64 machine running Linux with both G++ 4.9.1 and
 CLang 3.5.0. In both cases, the compiler switch -std=c++11 or -std=c++14
 has to be set in order to trigger the problem.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/10639>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:17 UTC