
| -----Original Message----- | From: boost-bounces@lists.boost.org | [mailto:boost-bounces@lists.boost.org] On Behalf Of Edward Diener | Sent: 14 March 2006 18:11 | To: boost@lists.boost.org | 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) giveup! 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 -- 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@hetp.u-net.com http://www.hetp.u-net.com/index.html http://www.hetp.u-net.com/Paul%20A%20Bristow%20info.html