This is the mail archive of the gcc@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]

FP consistency and -O2


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.


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