This is the mail archive of the gcc-prs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


>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:     jos@vandenoever.info
>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:


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]