|
From: Martin W. <mai...@ma...> - 2012-08-05 06:19:23
|
KHMan wrote: > On 8/5/2012 6:21 AM, Martin Whitaker wrote: >> After updating to the latest version of the MinGW GCC (4.7.0), one >> of my regression tests started failing. > > Interesting, it fails for me on MinGW gcc 3.4.5, MinGW 4.5.2 and > TDM gcc 4.5.2... > Hmmm, yes, the test case also fails for me if I go back to the previous version. The real code is of course more complex, so it's some other code generation change that has suddenly exposed this issue. > Search for 'pow' in your math.h header file and you will find the > answer in a long comment block: "Excess precision when using a > 64-bit mantissa for FPU math ops can cause unexpected results..." > > Adding: > > #include <fenv.h> > fesetenv(FE_PC53_ENV); > > gives 123 for the second line. Or save it first. The assembly > output around the pow() in the second line is: > > flds 32(%esp) > fstpl (%esp) > call _pow > fldl 16(%esp) ; loads 123000.0 > fdivp %st, %st(1) ; divide with pow() on x87 stack > > So the intermediate result was kept on the x87 stack, and the > final result isn't exact I suppose. > > glibc wraps the i386 pow instruction and test for various special > cases and conditions, including appropriate integer arguments, in > order to guarantee exact results where possible. > That explains it. I was misled by printing out the double value and seeing it was an exact integer. Saving the intermediate value in a temporary variable fixes the problem. Thanks! Martin |