Boost logo

Boost :

From: Paul Giaccone (paulg_at_[hidden])
Date: 2006-03-15 04:59:11


Edward Diener wrote:

>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.
>
>

Thanks for the feedback from everyone.

I should point out that this value (and all others) are not constants
but rather computed by my program. They therefore have exact
representations in binary, because they are the values of variables. If
I were trying to serialise and deserialise pi to 100 decimal places,
that would be a different matter, but these are plain old doubles. If I
were to serialise them using ordinary redirection to and from a text
file, I would imagine (I have not tried) that this would work. Maybe I
should check that as it would indicate whether the error is with the
assumption that the serialisation and deserialisation should be
symmetric rather than there being something wrong with the Boost library.

Paul


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk