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).
You can establish one of the following actions for a signal with a call to
signal
:
- Ignore the specified signal (identified by number).
- Use the default action for the specified signal, which can reset a previously established action.
- Transfer control from the specified signal to a procedure to receive the signal, specified by name.
In your case, you would want to write a function to do something when a floating point exception occurs (eg 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_exceptions
intrinsic module would be the portable way to do this, as suggested by High Performance Mark.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.