Boost logo

Boost Users :

From: Steven Solie (ssolie_at_[hidden])
Date: 2005-08-30 08:43:11


Scott McMurray wrote:
> > The std::equal() call is comparing an array of doubles using
> > operator == which I thought was impossible for floating point types
> > due to their inexact nature et al.
>
> A (radix 2) floating-point number will never be exactly equal to one
> seventh, for example, because it's not representable. The other issue
> is that roundoff error will make things such as 1./3*3 not result in
> exactly 1.
>
> That doesn't mean that you can't compare between them for equality.
> If one float is a copy of another, then they should be bitwise equal.
> Also, floating-point operations are deterministic, so the same
> sequence of operations -- in exactly the same environment -- should
> also yeild bitwise equal results.

What the random streaming operators are doing is saving and restoring
the state of the lagged_fibonacci607 object with text. This involves
converting the doubles into strings and back again.

I'm not certain if ISO C++ specifies whether we are allowed to assume
the conversion from double to string and back always yields the
identical floating point number or not.

Here is a small test program that demonstrates what the random test
suite is essentially doing:

#include <iostream>
#include <sstream>

int main()
{
    double pi = 3.14159;
    double pi2 = pi;

    if ( pi != pi2 ) {
        std::cout << "test failed, pi != pi2\n";
        return -1;
    }

    std::ostringstream file;
    file << pi;
    std::string ostr(file.str());

    std::istringstream input(file.str());
    input >> pi;
    std::string istr(input.str());

    if ( pi != pi2 ) {
        std::cout << "test failed, restored pi != pi2\n";
    }
    else {
        std::cout << "passed\n";
    }
}

This test always fails here.

--Steven


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net