g77_i386_fpe_demo, version 0.1

This small S/W package demonstrates handling of floating point exceptions (FPEs) under g77/i386/Linux. By default exceptions are not generated and serious errors such has sqrt(-ve number) and x/0.0 result in IEEE NaN and (+/-) INF propagating through the program.

Having learnt FORTRAN programming under another platform (DEC VMS) where serious floating point errors always generate exceptions and the program terminates, I found the default behaviour under g77/i386/Linux quite disconcerting.

There are several tips to be found on Usenet and on the gcc website, eg. http://gcc.gnu.org/onlinedocs/gcc-3.0.3/g77_20.html#SEC643 for dealing with this situation but no complete worked examples showing the creation and usage of FPE handlers under g77/i386/Linux.

This package comprises two simple FORTRAN calling programs which demonstrate handling and trapping the six types of FPEs generated by the i387 FPU in accordance with the IEEE standard, IEEE-754, namely,

(1) Invalid operation
(2) Denormalised result
(3) Division by zero
(4) Overflow
(5) Underflow
(6) Precision loss (inexact result)

Probably, the most useful default settings are to trap exceptions; 1, 3 & 4 above.

Alternatively, when running the programs under the GNU debugger gdb, gdb's own exception handlers trap the FPEs. Debugger commands such as 'list' and 'bt' can be used to locate the line/calling sequence which generated the exception.  The 'info float' command can then be used to examine the status of the FPU.

The tarball (version 0.1) is here .

This software is released under the GNU GPL.

Programmers interested in doing more sophisticated things regarding handling FPEs should investigate Bill Metzenthen's wmexcp package.

Added: 18th May 2005
It is worth adding that g95 now supports easy trapping of FPEs via environment variables. See the manual for details.  Eg. defining G95_FPU_INVALID will cause the runtime to display a message and exit when a NaN occurs.  Running under gdb will give the file/line number where the FPE occurred.

Tom Crane, May 2005