public inbox for gcc-prs@sourceware.org help / color / mirror / Atom feed
From: jos@vandenoever.info To: gcc-gnats@gcc.gnu.org Subject: c++/10902: comparing doubles is not consistent in gcc 3.3 (SuSE version) Date: Wed, 21 May 2003 10:33:00 -0000 [thread overview] Message-ID: <20030521101816.28973.qmail@sources.redhat.com> (raw) >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:
next reply other threads:[~2003-05-21 10:33 UTC|newest] Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top 2003-05-21 10:33 jos [this message] 2003-05-21 11:36 Jos van den Oever 2003-05-21 12:36 Jos van den Oever 2003-05-21 14:14 bangerth 2003-05-21 14:36 Jos van den Oever 2003-05-21 14:46 Wolfgang Bangerth 2003-05-21 14:56 Jos van den Oever
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20030521101816.28973.qmail@sources.redhat.com \ --to=jos@vandenoever.info \ --cc=gcc-gnats@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).