Boost logo

Boost :

From: Paul A Bristow (pbristow_at_[hidden])
Date: 2006-03-14 14:15:52


| -----Original Message-----
| From: boost-bounces_at_[hidden]
| [mailto:boost-bounces_at_[hidden]] On Behalf Of Edward Diener
| Sent: 14 March 2006 18:11
| To: boost_at_[hidden]
| Subject: Re: [boost] [serialization]
| Serialisation/deserialisation of floating-point values
| Paul Giaccone wrote:
| > I'm having problems with deserialising floating-point
| (double) values
| > that are written to an XML file. I'm reading the values
| back in and
| > comparing them to what I saved to ensure that my file has
| been written
| > correctly. However, some of the values differ in about the
| seventeenth
| > significant figure (or thereabouts).
| >
| > I thought Boost serialization used some numerical limit to
| make sure
| > that values are serialised exactly to full precision, so what is
| > happening here?
| This is a common cause of errors when using floating point values.
| Writing a floating point value to a string representation, as are XML
| values, and attempting to read that string representation
| back, does not
| guarantee that the floating point value will remain exactly the same
| since there are a number of floating point values which have no exact
| representation in the C++ floating point formats. That is
| simply because
| of the nature of floating point representation used in C++ and most
| modern languages. After all, the number of floating point
| values within
| any range of numbers is infinite while the C++ floating point
| representation cab not be. The only way to guarantee what you
| want for
| floating point values is to write and read back to a binary
| representation of the value.

I think that this is unduly pessimistic.

In practice, provided the archive is written and read where floating point
have the same IEEE format, usually 64-bit doubles, it should work OK.

You could check in your program by reading

if (std::numeric_limits<long double>::digits != 53)


 that this is so, and warn if it is not.

If the binary representation is different, it won't work anyway! So you
would be any better off!


Paul A Bristow
Prizet Farmhouse, Kendal, Cumbria UK LA8 8AB
Phone and SMS text +44 1539 561830, Mobile and SMS text +44 7714 330204
mailto: pbristow_at_[hidden]

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