Boost logo

Boost :

From: Paul A Bristow (pbristow_at_[hidden])
Date: 2006-03-15 05:54:09


If you want to test round-tripping 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.u-net.com/index.html
http://www.hetp.u-net.com/Paul%20A%20Bristow%20info.html
| -----Original Message-----
| From: boost-bounces_at_[hidden] 
| [mailto:boost-bounces_at_[hidden]] On Behalf Of John Maddock
| Sent: 14 March 2006 19:23
| To: boost_at_[hidden]
| Subject: Re: [boost] [serialization] 
| Serialisation/deserialisation offloating-point values
| 
| While it's true that some decimal values have no exact binary 
| representation 
| and vice-versa, 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 round-trip numbers in this way (I know because 
| I've reported 
| these as bugs!), getting round-trip binary-decimal-binary 
| conversion right 
| is actually pretty hard.
| 
| The classic "What Every Computer Scientist Should Know About 
| Floating-Point 
| Arithmetic" at 
| http://docs.sun.com/source/806-3568/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 p-digit 
| 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 binary-decimal 
| 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