gcc floating point bug(?) update + Source
Christoph Probst
chris-ml-gcc-bugs@netzpunkt.org
Tue Jan 21 21:12:00 GMT 2003
Hi Juggy.
juggy@gmx.net wrote:
> (2) tstbug.c:
> When I tried to isolate the error in my software I first wrote this
> little program. Though I wasn't able to reproduce the exact same
> behaviour, but some other oddities showed up.
I minimized the tstbug.c code to get the error more obvious:
/--[ code sample gcc-test.c ]-----------------------------------------
#include <math.h>
double a, b, c, d = +3.247778884435419843867975941975601017475128173828125E+00;
int main() {
a = -3.1493999999999999772626324556767940521240E+01/d;
if (fabs(a)> fabs(d)) {
b = 1;
}
printf("%+1.40E\n", 31.49 + a * 3.247778884435419843);
return 0;
}
\---------------------------------------------------------------------
gcc -O1 gcc-test.c ; ./a.out
-3.9999999999989436366698569713662436697632E-03
gcc -O2 gcc-test.c ; ./a.out
-4.0000000000013340856197530115423433016986E-03
The results differ depending on the compiler optimization level.
One might say this is a normal rounding error, but how would you explain
the following behaviour? Make little changes to the above code
e.g. assign d inside the main()-function and not before:
------------------------------------8<---------------------------------
double a, b, c, d;
int main() {
d = +3.247778884435419843867975941975601017475128173828125E+00;
a = -3.1493999999999999772626324556767940521240E+01/d;
...
------------------------------------8<---------------------------------
OR add a simple printf("test") to the if-statement:
------------------------------------8<---------------------------------
...
if (fabs(a)> fabs(d)) {
printf("test");
b = 1;
}
...
------------------------------------8<---------------------------------
and the rounding error disappears:
gcc -O1 gcc-test.c ; ./a.out
-3.9999999999989436366698569713662436697632E-03
gcc -O2 gcc-test.c ; ./a.out
-3.9999999999989436366698569713662436697632E-03
Testet on Gentoo Linux, Kernel 2.4.20, gcc version 2.95.3 20010315
Tests with gcc-3.2.1 and tests on FreeBSD machines point out no error
at all ...
HTH
Chris
--
"Beware of bugs in the above code; I have only proved it correct,
not tried it." - Donald Knuth
More information about the Gcc-bugs
mailing list