Bug!!
Toon Moene
toon@moene.indiv.nluug.nl
Tue Jan 15 15:43:00 GMT 2002
Dick Arndt wrote:
> I am running gcc version 2.96 20000731 with a g77(or f77) command and
> have discovered the following logical disaster: (run the following pgm
> with g77 -o xxxx xxx.f )
>
> c Program xxxx
> a=193.15
> b=0.1
> c=a+b
> if(a+b.ne.c) write(*,100) a,b,c
> 100 format(3f20.9)
> stop
> end
>
> The ne if is satisfied and the write proceeds. Apparently, arithmetic
> done inside an "if" statement is done to a different precision than that
> done outside the "if" statement.
Indeed. See http://gcc.gnu.org/onlinedocs/gcc-3.0.2/g77_19.html#SEC594
in our documentation for how to deal with that.
I think I take the opportunity to insert a firm warning for those who
would like to compare floating point values for (in)equality in Fortran,
though.
The above example is a valid way to compute a value into variable `c'
and compare it to the value of the original expression `a+b'. That this
doesn't always work with g77 is a "quality of implementation issue" with
this compiler.
However, given that the above example is correct and should not execute
the `write' statement, it is tempting to think that, e.g.:
a=193.15
b=0.1
c=243.2
d=a+b+c
if(a+b+c.ne.d)write(*,*)'Bug!!'
end
should also not print anything. Unfortunately, no such guarantee is
given by the Fortran Standard(s). It's up to the compiler to generate a
convenient way to evaluate the expression `a+b+c' and it is certainly
not obliged to evaluate two occurrences of this expression in the same
way.
If you need to force the order of evaluation, use parentheses.
Thank you for your time.
--
Toon Moene - mailto:toon@moene.indiv.nluug.nl - phoneto: +31 346 214290
Saturnushof 14, 3738 XG Maartensdijk, The Netherlands
Maintainer, GNU Fortran 77: http://gcc.gnu.org/onlinedocs/g77_news.html
Join GNU Fortran 95: http://g95.sourceforge.net/ (under construction)
More information about the Gcc-bugs
mailing list