
Boost : 
From: Paul A Bristow (pbristow_at_[hidden])
Date: 20060315 05:54:09
If you want to test roundtripping on your platform and std lib without
actually using serialization,
may I suggest a loop including something like:
double a = some start value;
double aa; // to hold the read back.
std::stringstream s;
s.precision(2+std::numeric_limits<double>::digits * 3010/10000);
// cout << "output " << a;
s << a; // output to string s
//cout << ", s.str() is " << s.str();
s >> aa; // read back in.
//cout << ", read back " << aa << endl;
if (a != aa)
{
cout << "error " << a << tab << aa << endl;
}
a = nextafter(a, std::numeric_limits<double>::max()); // Make one
bit bigger?
of course this may take too long for the full range of possible double! 
some years ;))
Took overnight for al possible floats on my aging system.
//a *= 10.; // And times 10 too to make test run in reasonable
time. Ran OK 8.0 16 sep 04
This should give you a feel for the risk of failure.
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_at_[hidden] http://www.hetp.unet.com/index.html http://www.hetp.unet.com/Paul%20A%20Bristow%20info.html  Original Message  From: boostbounces_at_[hidden]  [mailto:boostbounces_at_[hidden]] On Behalf Of John Maddock  Sent: 14 March 2006 19:23  To: boost_at_[hidden]  Subject: Re: [boost] [serialization]  Serialisation/deserialisation offloatingpoint values   While it's true that some decimal values have no exact binary  representation  and viceversa, I believe you *should* be able to write as a  decimal string  and read back in, and get the same value, provided:   * You write enough digits to the file,  numeric_limits<T>::digits + 2 seems  to be enough, but I wouldn't want to guarantee that.  * You're std lib is bug free: there certainly have been cases  of std lib's  that don't roundtrip numbers in this way (I know because  I've reported  these as bugs!), getting roundtrip binarydecimalbinary  conversion right  is actually pretty hard.   The classic "What Every Computer Scientist Should Know About  FloatingPoint  Arithmetic" at  http://docs.sun.com/source/8063568/ncg_goldberg.html fills  in the details: 9 decimal digits are required for single  precision reals,  and 17 for double precision, a formula is also given that  allows you to  check that you have enough decimal digits for some pdigit  binary number.  It's also apparent that reading in a decimal number correctly  requires  extended precision arithmetic, so I suspect most problems are  likely to  occur when serialising the widest floating point type on the  system. Even  Knuth says "leave it to the experts" when discussing binarydecimal  conversion BTW :)   HTH, John.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk