This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
FP consistency and -O2
- From: Andrew Haley <aph at redhat dot com>
- To: Stuart Reynolds <gcc at stuartreynoldsDOTnet dot cambridge dot redhat dot com>
- Cc: gcc at gcc dot gnu dot org
- Date: Tue, 6 Jan 2004 20:24:15 +0000
- Subject: FP consistency and -O2
- References: <3FFB1480.9080901@stuartreynoldsDOTnet>
Stuart Reynolds writes:
> 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 an FAQ.
gcc uses the floating-point unit's extended precision for temporaries,
so if you really need to lose precision you have to save into a
temporary variable and use -ffloat-store. But it's not very often
that you really, really need to lose precision.
Try this:
#include <iostream>
#include <iomanip>
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;
float f = std::cos(x);
cout << i << '\t' << f << endl;
}
return 0;
}
Andrew.