I’m using gfortran for some code. For a while now, I’ve been compiling with
-ffpe-trap=zero,overflow,invalid
in an attempt to hunt down some bugs. This causes my program to cease execution immediately. There are some cases where the FPE might be OK and so a flag like:
-ffpe-warn=zero,overflow,invalid
would be very useful. Does gfortran (or any other compiler) provide anything like this? If not, are there any workarounds? My current thought is to create a C function to register a signal handler to write out the warning, although I have no idea how to go about doing that.
I don’t know of a way of warning on encountering a floating point exception. But both gfortran and ifort have signal handling routines. See for example the gfortran documentation of signal and
the Intel Fortran Compiler User and Reference Guides (warning: large PDF) (see page 410 on wards).
In your case, you would want to write a function to do something when a floating point exception occurs (e.g. print file name/line number), and use the third option in the above list.
Unfortunately this is not very portable: take a look at this page for examples of signal handling for various compilers. You could wrap some code in preprocessor macros if you want to
-NDEBUG)Update: Ultimately the exception handling facilities of the
ieee_exceptionsintrinsic module would be the portable way to do this, as suggested by High Performance Mark.