floating point inconsistency

Andrew Haley aph@redhat.com
Tue Feb 16 11:48:00 GMT 2010


On 02/16/2010 12:29 AM, Christoph Groth wrote:
> 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!

That is strange.  A bit of debugging would be required to understand
why that happens.

> 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.

gcc applies an optimization to code where the same angle is passed to
sin and cos.  This is turned into a single call to sincos() aka
__builtin_cexpi().  Clearly the lsb is different in the optimized
case.

Andrew.



More information about the Gcc-help mailing list