I run across an issue related to different branching when various levels of optimization are specified. I've encapsulated an example of the behavior in the simple C++ code attached at the bottom of this note. The problem appease both on a standard RedHat 9.0 and Fedora Core 1 release. The following shows the output of gcc --version: [pkb@salsa tool]$ gcc --version gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5) Copyright (C) 2002 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. [pkb@salsa tool]$ gcc -v Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux Thread model: posix gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5) [root@quesadilla tmp]# gcc --version gcc (GCC) 3.3.2 20031022 (Red Hat Linux 3.3.2-1) Copyright (C) 2003 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. [root@quesadilla tmp]# gcc -v Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.3.2/specs Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux Thread model: posix gcc version 3.3.2 20031022 (Red Hat Linux 3.3.2-1) The following shows the output from the attached example based upon the optimization level (the first run produces the correct output): [root@quesadilla tmp]# g++ t.cc [root@quesadilla tmp]# ./a.out same on first check same on first check same on first check same on first check same on first check same on first check same on first check same on first check same on first check same on first check [root@quesadilla tmp]# g++ -O2 t.cc [root@quesadilla tmp]# ./a.out [root@quesadilla tmp]# g++ -O3 t.cc [root@quesadilla tmp]# ./a.out [root@quesadilla tmp]# g++ -O1 t.cc [root@quesadilla tmp]# ./a.out now they are the same! [root@quesadilla tmp]# Here is the example, which encapsulates the problem (this is not real code - just my way of taking the problem that showed up and putting it into a simple C++ file for demonstration purposes): #include <cstdlib> #include <iostream> using namespace std; double foo() { static double next=1.1; static bool computeNext=true; if (computeNext) { next *= std::sqrt(next); } computeNext = !computeNext; return next; } int main(int argc, char** argv) { for (int j = 0; j < 10; j++) { // double ong = foo(); // double cng = foo(); double ong = std::sqrt(7.0); double cng = std::sqrt(7.0); if (ong == cng) { std::cerr << "same on first check\n"; } else if (ong != cng) { for (double k = 1.1; k < 10; k *= std::sqrt(k)); if (ong == cng) { std::cerr << "now they are the same!\n"; return 1; } } else { std::cerr << "not same, then they were?\n"; return 2; } } return 0; } Please feel free to contact me with any questions.
So are the joys of floating point computations. This is a duplicate of PR 323. W. *** This bug has been marked as a duplicate of 323 ***