HP-UX Floating-Point Guide

Chapter 6 153
Floating-Point Trap Handling
Enabling Traps
Enabling Traps
When you enable a trap without providing a trap handler (a mechanism
for handling it), the trap causes a SIGFPE signal. The signal, in turn,
causes your program to abort with an error message that is more or less
informative, depending on the method you use to enable the trap. If you
want your program to abort when it encounters a trap, then enabling the
trap may be all you want to do. However, you may want to handle the
trap gracefully; see “Handling Traps” on page 157 for information on
trap handling.
HP 9000 systems provide several methods of enabling traps:
The +FP compiler option (all compilers)
The fesettrapenable function
The +fp_exception (Fortran 90) and +T (HP FORTRAN/9000)
compile-line options
We discuss these briefly in the following subsections.
Using the +FP Compiler Option
The +FP option, described in “Command-Line Mode Control: The +FP
Compiler Option” on page 148, allows you to enable traps from the
compiler command line. No change in your program is required.
The disadvantage of using this option is that you cannot know exactly
where in your program the exception occurred. Moreover, unless you
enable a trap for only one exception, you cannot know the type of
exception that occurred. You merely get a core dump. The following
Fortran program, for example, generates an overflow.
Sample Program: overflow.f
PROGRAM OVERFLOW
DOUBLE PRECISION X, Y, Z
X = 1.79D308
Y = 2.2D-308
Z = X / Y
PRINT 30, X, Y, Z
30 FORMAT (1PE11.4, ' divided by', 1PE11.4, ' = ', 1PE11.4)
END