Boost C++ Libraries: Ticket #6953: Cannot serialialize some double values https://svn.boost.org/trac10/ticket/6953 <p> Hi, </p> <p> The code sample below throws a "stream error" exception at line </p> <p> ia &gt;&gt; double_back; </p> <p> My development environment is Visual Studio 10, boost version 1.43. Sorry, I have not tried a newer version of boost. </p> <p> The binary archive does not seem to handle doubles with lots of decimal places. </p> <p> #include &lt;boost/archive/binary_oarchive.hpp&gt; #include &lt;boost/archive/binary_iarchive.hpp&gt; </p> <p> void dummy() </p> <blockquote> <p> { </p> </blockquote> <p> </p> <blockquote> <blockquote> <p> string filename = "dummy.data"; </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> double someDouble = 0.98123024735605502; </p> </blockquote> </blockquote> <p> </p> <blockquote> <blockquote> <p> { </p> <blockquote> <p> std::ofstream ofs; ofs.open(filename.c_str()); boost::archive::binary_oarchive oa(ofs); oa &lt;&lt; someDouble; </p> </blockquote> <p> } </p> </blockquote> </blockquote> <p> </p> <blockquote> <blockquote> <p> double double_back; { </p> <blockquote> <p> std::ifstream ifs; ifs.open(filename.c_str()); boost::archive::binary_iarchive ia(ifs); <em> following throws a "stream error" exception ia &gt;&gt; double_back; </em></p> </blockquote> <p> } </p> </blockquote> </blockquote> <blockquote> <p> } </p> </blockquote> <p> Thanks, </p> <p> Dragos </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/6953 Trac 1.4.3 Robert Ramey Sun, 15 Jul 2012 20:01:57 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/6953#comment:1 https://svn.boost.org/trac10/ticket/6953#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> I'm gueesing that is not an issue with the serialization library but rather floating point i/o when converting to text. Rework you example to use just standard stream i/o. </p> <p> your test case .98123024735605502 has 17 significant decimal digits. This is approximatly 58 binary digits. Which shouldn't be a problem on an 80 bit IEEE754 implementation. But it could be a problem with a 64 bit implementation. </p> <p> Also, some decimal fractional numbers might not be exactly representable in binary. This would lead to a truncation or rounding when he number is converted to binary. This would only be noticable on output. </p> <p> I'm pretty sure I'm right here so I'm going to close this ticket. </p> <p> Robert Ramey </p> Ticket