This is the mail archive of the
fortran@gcc.gnu.org
mailing list for the GNU Fortran project.
Re: -ffast-math and gfortran
Steve,
> On Sunday 04 June 2006 18:58, Steve Kargl wrote:
> Go back in the thread and see the very simple program I wrote.
> It is well-behaved without -ffast-math but generates NaNs with
> this option.
>
> Apparently, you do not use complex data types.
I do sometimes.
> Or, do you expand all multiplications and divisions into the
> well-known numerically stable algorithms?
>
Yes, I always do. Based on the experience that in the long
run it saves you time hunting for "unexpected" behavior.
But one does not need to go to complex math to find
examples where "well-behaved" programs fail.
Everybody who writes code that deals with floating
point arithmetic should understand the limitations posed
by their limited precision and range.
To come back to your example:
program h
complex z
z = cmplx(1.e-25,1.e-25)
call div(z,z)
end program h
subroutine div(a,b)
complex a, b, c
c = a / b
print *, c
end subroutine div
gfortran h.f90 && ./a.out
( 1.000000 , 0.000000 )
gfortran -ffast-math h.f90 && ./a.out
( NaN, NaN)
gfortran -fno-math-errno -funsafe-math-optimizations -fno-trapping-math -ffinite-math-only -fno-rounding-math -fno-signaling-nans
h.f90 && ./a.out
( 1.000000 , 0.000000 )
The last line uses all flags set by -ffast-math except fcx-limited-range.
Someone who does not understand why your program behaves the way
it does with the different options should look for another job. ;-)
As long as the fast math option is not removed altogether
and the switch change is documented, your can call
it -frisky-math or whatever else you like :-)
Ivan