HP-UX Floating-Point Guide
Chapter 6 159
Floating-Point Trap Handling
Handling Traps
Sample Program: overflow_on.f
PROGRAM OVERFLOW_ON
DOUBLE PRECISION X, Y, Z
ON DOUBLE PRECISION OVERFLOW CALL HANDLE_OFL
X = 1.79D308
Y = 2.2D-308
Z = X / Y
WRITE(*,*) X, ' divided by', Y, ' = ', Z
END
SUBROUTINE HANDLE_OFL(A)
DOUBLE PRECISION A, T
INTEGER I
WRITE(*,*) 'overflow occurred'
WRITE(*,*) 'argument to HANDLE_OFL is ', A
C The result is 2**1536 too small
T = LOG10(A) + 1536 * LOG10(2D0)
I = T ! Get exponent
T = T - I
WRITE(*,*) 'the correct answer is', 10**T,
x ' times 10 to the power', I
STOP
END
See the PA-RISC 1.1 Architecture and Instruction Set Reference Manual
for a full explanation of the bias of 1536; when traps are enabled, the
IEEE standard requires a biased result to be returned for overflow and
underflow.
If you compile and run this program using HP Fortran 90 and HP
FORTRAN/9000 respectively, it produces the following result:
$ f90 overflow_on.f
overflow_on.f
program OVERFLOW_ON
external subroutine HANDLE_OFL
26 Lines Compiled
$ ./a.out
overflow occurred
argument to HANDLE_OFL is 3.375646885228544E+153
the correct answer is 8.13636363636275 times 10 to the power 615
$ f77 +T overflow_on.f
overflow_on.f:
MAIN overflow_on:
handle_ofl:
$ ./a.out
overflow occurred
argument to HANDLE_OFL is 3.375646885228544+153
the correct answer is 8.13636363636275 times 10 to the power 615
One situation where it is useful to assign a value to the trap handler
argument and continue program execution is that of an underflow
exception (described in “Underflow Conditions” on page 57). Substituting