HP-UX Floating-Point Guide

160 Chapter 6
Floating-Point Trap Handling
Handling Traps
a value of 0 for the result of an operation that underflows may be exactly
what you want to do. In fact, the system may perform this substitution
for you; the IEEE standard specifies that 0 may be the result of an
operation that underflows, and on HP 9000 systems it often is (when the
result of the operation is less than the smallest denormalized value). If
you want to guarantee a result of 0, you can call a handler as follows:
Sample Program: underflow_on.f
PROGRAM UNDERFLOW_ON
DOUBLE PRECISION X, Y, Z
ON DOUBLE PRECISION UNDERFLOW CALL HANDLE_UFL
X = 1.0D0
Y = 1.79D308
Z = X / Y
PRINT 30, X, Y, Z
30 FORMAT (1PE11.4, ' divided by', 1PE11.4, ' = ', 1PE11.4)
END
SUBROUTINE HANDLE_UFL(A)
DOUBLE PRECISION A
WRITE(*,*) 'underflow occurred'
A = 0.0
RETURN
END
If you compile and run this program, it produces the following result:
$ f90 underflow_on.f
underflow_on.f
program UNDERFLOW_ON
external subroutine HANDLE_UFL
21 Lines Compiled
$ ./a.out
underflow occurred
1.0000E+00 divided by 1.7900+308 = 0.0000E+00
$ f77 +T underflow_on.f
underflow_on.f:
MAIN underflow_on:
handle_ufl:
$ ./a.out
underflow occurred
1.0000E+00 divided by 1.7900+308 = .0000E+00
If your system supports fast underflow mode, you can use it both to
guarantee a result of 0 for all underflows and to avoid the overhead of
incurring a trap. You can enable fast underflow mode with either the +FP
option (see “Command-Line Mode Control: The +FP Compiler Option” on
page 148) or the fesetflushtozero routine (see “Underflow Mode:
fegetflushtozero and fesetflushtozero” on page 145).