This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
FP consistency and -O2
- From: Stuart Reynolds <gcc at stuartreynoldsDOTnet>
- To: gcc at gcc dot gnu dot org
- Date: Tue, 06 Jan 2004 12:03:12 -0800
- Subject: FP consistency and -O2
I'm having issues with the optimisation options. I get different
floating point calculations depending on whether -O is set or not.
Is this a bug? It's certainly causing bugs for me.
If its not a bug, does anyone know if there a way of turning off all
optimisations that change program semantics?
Cheers,
Stuart
Here's the offending code:
--------------
#include <iostream>
#include <iomanip>
/*
Run:
g++ format.cxx && a.out > format.txt
g++ -O2 format.cxx && a.out > format_O2.txt
diff format_O2.txt format.txt
Output differs. Same with -O and -O1
gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
*/
using namespace std;
int main()
{
static const float period = 2 * M_PI;
static const float totsteps = 50.0; //per period
cout << setprecision(6) << setiosflags(ios::fixed | ios::showpoint);
for ( int i=0 ; i < 1000; ++i )
{
float x = ( period * i ) / totsteps;
cout << i << '\t' << std::cos(x) << endl;
}
return 0;
}