Boost logo

Boost :

Subject: [boost] [serialization] round-trip serialization of float
From: Adam Lerer (adam.lerer_at_[hidden])
Date: 2013-01-09 17:26:02

The boost serialization of float doesn't appear to use enough digits to
guarantee that deserialization of a serialized value will produce the same
value. The relevant code is in boost/archive/basic_text_oprimitive.hpp:

void save(const float t)
    os << std::setprecision(std::numeric_limits<float>::digits10 + 2);

std::numeric_limits<float>::digits10+2=8, but float actually requires
numeric_limits<float>::max_digits10=9 decimal digits to guarantee unique

The code below (with boost/1.52) demonstrates the problem:

#include <sstream>
#include "boost/archive/text_iarchive.hpp"
#include "boost/archive/text_oarchive.hpp"
#include <iostream>

int main(int argc, char **argv){
    float oldf = -1.20786635e-05;
    std::ostringstream os;
    boost::archive::text_oarchive oa(os);

    float newf;
    std::istringstream is(os.str());
    boost::archive::text_iarchive ia(is);
    printf("before serialization: %.12g\nafter serialization: %.12g\n",
oldf, newf);

// Output:
// before serialization: -1.20786635307e-05
// after serialization: -1.20786644402e-05

Note: There's some discussion of this topic in a slightly different context

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