Mail Archives: djgpp/1995/01/02/03:22:12
I have a paradox I cannot understand. I have compiled a program under
GCC and under Borland TC (v. 2.0) and TC++ (v. 3.1) for Windows,
and both versions give results quite different, being the GCC version
the correct one. I include a simple code that represent the simplifie=
d
core of the problem: a double obtained from a product and stored subt=
racted
=66rom the same product does not give 0 as result. It seems an error =
of
Borland compilers but I feel it to serious to be true; I'd like to kn=
ow
if it is a real bug or if it has some justification (although the res=
ult
is erroneus) and if there is some way to skip the error without affec=
ting
the code performace.
Here is the source code:
=09#include <stdio.h>
=09main()
=09{
=09double TM4, TM3=3D-0.0011025;
=09=09TM4 =3D TM3*4.8E6;
=09=09printf ("TM4 seems correctly calculated and stored; ");
=09=09printf ("it is equal to TM3*4.8E6:\n");
=09=09printf (" -> TM4 =3D %.16e; TM3*4.8E6 =3D %.16e\n\n", TM4, TM3=
*4.8E6);
=09=09printf ("Nevertheless, the subtraction gives a wrong result:\n"=
);
=09=09printf (" -> TM4-TM3*4.8E6 =3D %.16e =3D %.16f\n\n",
=09=09=09=09=09=09=09=09=09TM4-(TM3*4.8E6), TM4-(TM3*4.8E6));
=09=09printf ("But if we do the subtraction in this way the result ")=
;
=09=09printf ("is correct:\n");
=09=09printf (" -> TM3*4.8E6-TM3*4.8E6 =3D %.16e =3D %.16f",
=09=09=09=09=09=09(TM3*4.8E6)-(TM3*4.8E6), (TM3*4.8E6)-(TM3*4.8E6));
=09=09return 0;
=09}
Here is the output on a Borland compiler:
=09TM4 seems correctly calculated and stored; it is equal to TM3*4.8E=
6:
=09 -> TM4 =3D -5.292000000000000e+03; TM3*4.8E6 =3D -5.292000000000=
000e+03
=09Nevertheless, the subtraction gives a wrong result:
=09 -> TM4-TM3*4.8E6 =3D -1.709743457922741e-13 =3D -0.0000000000001=
710
=09But if we do the subtraction in this way the result is correct:
=09 -> TM3*4.8E6-TM3*4.8E6 =3D 0.000000000000000e+00 =3D 0.000000000=
0000000
And here is the GCC output, that is Ok:
=09TM4 seems correctly calculated and stored; it is equal to TM3*4.8E=
6:
=09 -> TM4 =3D -5.2920000000000000e+03; TM3*4.8E6 =3D -5.29200000000=
00000e+03
=09Nevertheless, the subtraction gives a wrong result: /* Correct on =
GCC */
=09 -> TM4-TM3*4.8E6 =3D 0.0000000000000000e+00 =3D 0.00000000000000=
00
=09But if we do the subtraction in this way the result is correct:
=09 -> TM3*4.8E6-TM3*4.8E6 =3D 0.0000000000000000e+00 =3D 0.00000000=
00000000
I have run the program on a i486 machine.
Thank you for your explanations.
Xavier Pons
Centre de Recerca Ecol=95gica i Aplicacions Forestals
Facultat de Ciencies. Universitat Autonoma de Barcelona.
08193 Bellaterra.
Catalonia. Spain.=1A
- Raw text -