Boost logo

Boost :

Subject: [boost] [serialization]Double rounding bug?
From: Mariusz Kwiczala (mariusz.kwiczala_at_[hidden])
Date: 2009-10-01 15:09:00


I found that for some small double value, serialization change last few
digits.(it look like some rounding bug)
Below is one example (I have more :) usually it is a number 0.00... or 0.00
with 19-20 digits after dot):
-0.0070850229047457131 - double value
-0.007085022904745714 - double value after save/read from xml file
On my computer I have always assert in function DoubleTest().

Boost: version 1.39/boost_serialization-vc90-mt-gd-1_39.dll
Compiler: VS2008

Could you verify below code on your computer (call DoubleTest())?
Is it bug?

Sample code:
#pragma warning( push )
#pragma warning( disable : 4512 )
#include <boost/archive/xml_iarchive.hpp>
#pragma warning( pop )
#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/bind.hpp>
#include <fstream>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/vector.hpp>

void LoadDoubleValue(DoubleValue& doubleValue, const char* fileName)
   std::ifstream ifs(fileName);
      xml_iarchive ia(ifs);

   ia >> BOOST_SERIALIZATION_NVP(doubleValue);

void SaveDoubleValue(DoubleValue& doubleValue, const char* fileName)
   std::ofstream ofs(fileName);
   boost::archive::xml_oarchive oa(ofs);

   oa << BOOST_SERIALIZATION_NVP(doubleValue);

void DoubleTest()
   DoubleValue doubleValue1;
   doubleValue1.m_value = -0.0070850229047457131;
   SaveDoubleValue(doubleValue1, "DoubleValue.xml");

   DoubleValue doubleValue2;
   LoadDoubleValue(doubleValue2, "DoubleValue.xml");
   //but from file is read -0.007085022904745714

Best regards,

Boost list run by bdawes at, gregod at, cpdaniel at, john at