floating point inconsistency

Christoph Groth cwg@falma.de
Tue Feb 16 00:43:00 GMT 2010


Hello,

using g++ 4.3.4 (on Debian testing) I discovered the following problem.

I compile the program

################################################################
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

double length = 0.00106722283403500495;
double angle = 0.60066412215710574;

struct Vec {
    double x[2];
};

int main()
{
    cout << setprecision(18);
    Vec vec = { cos(angle) * length, sin(angle) * length };
    cout << vec.x[0] << ' ' << vec.x[1] << endl;
    cout << cos(angle) * length << endl;
    cout << vec.x[0] - cos(angle) * length << endl;
}
################################################################

with

g++ -O0 -static test.cc

and it produces

0.000880416620183556821 0.000603184177834822054
0.000880416620183556821
0

However, with

g++ -O1 -static test.cc

it produces

0.00088041662018355693 0.000603184177834822054
0.000880416620183556821
1.08420217248550443e-19

This is on a computer with an Intel Core 2 CPU (I tried several
different variants).

If I take the second (-O1) binary and execute it on an Opteron it
produces the _first_ result.  Thus, the same static binary produces
different results on different processors!

I observe this problem also with g++ 4.4.2.  It seems only a tiny
difference, but this inconsistency makes my Montecarlo simulation
produce different results on different machines.

Any help with this would be greatly appreciated.

Christoph



More information about the Gcc-help mailing list