
On 3/15/06, Janek Kozicki <janek_listy@wp.pl> wrote:
try following modifications in above code. If you still get a mistake by one bit, then .... well, I'd be very surprised.
Prepare to be suprised. Here's the exact code I compiled: #include <iostream> #include <sstream> #include <cassert> #include <boost/lexical_cast.hpp> int main () { std::stringstream stream; double orig_value = 0.0019075645054089487; stream << boost::lexical_cast<std::string> (orig_value); double num = boost::lexical_cast<double> (stream.str()); assert (num == orig_value); } On gcc + Linux this fails: lc: lc.cpp:12: int main(): Assertion `num == orig_value' failed. Breakpoint 1, main () at lc.cpp:12 12 assert (num == orig_value); (gdb) print num $3 = 0.0019075645054089489 (gdb) print orig_value $4 = 0.0019075645054089487 On MSVC 8, the program also asserts and the values are similarly mismatched: orig_value 0.0019075645054089487 double num 0.0019075645054089489 double Note that boost::lexical_cast uses a precision of std::numeric_limits<T>::digits10 + 1 in its T-to-string conversions. For double, this is 16 which would probably explain the mismatch on the 17th significant digit on two separate platforms. -- Caleb Epstein caleb dot epstein at gmail dot com