c++/10902: comparing doubles is not consistent in gcc 3.3 (SuSE version)

[email protected]
Newsgroups gmane.comp.gcc.prs
Message-ID <20030521101816.28973.qmail__35337.0070377879$1053513172@sources.redhat.com>
>Number:         10902
>Category:       c++
>Synopsis:       comparing doubles is not consistent in gcc 3.3 (SuSE version)
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Wed May 21 10:26:00 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     [email protected]
>Release:        unknown-1.0
>Organization:
>Environment:
SuSE 8.2 i386
>Description:
When comparing doubles without storing them in a temporary variable, the comparison can go wrong. The code below compares two doubles. One of them has value 1, the other has the value of the largest value below 1. When comparing these numbers, the difference is found.
But when comparing these numbers without storing the result, the comparison is apparently performed in float precision.

#include <iostream>
using std::cout;
using std::endl;
#include <cfloat>

int
main() {
        double one = double(1);
        double belowone = double(1)-DBL_EPSILON;

        if (one == belowone) {
                cout << "one == belowone" << endl;
        } else {
                cout << "one != belowone" << endl;
        }

        if (double(1) == double(1) - DBL_EPSILON) {
                cout << "double(1) == double(1) - DBL_EPSILON" << endl;
                int i=1;
                while (double(1) == double(1) - i*DBL_EPSILON) ++i;
                cout << "double(1) == double(1) - "<<i-1<<"*DBL_EPSILON"
                        << endl;
                cout << "This comparison is not OK!" << endl;

        } else {
                cout << "double(1) != double(1) - DBL_EPSILON" << endl;
        }

        return 0;
}
>How-To-Repeat:
Compile the code given above. I find the bug with gcc version 3.3 20030226 (prerelease) (SuSE Linux), irrespective of using -O or -g.

When using gcc version 2.95.3 20010315 (SuSE), the bug is not present.
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.