The -fpen (Linux* and Mac OS* X) or /fpe:n (Windows*) option allows some control over the results of floating-point exceptions.
-fpe0 or /fpe:0 restricts floating-point exceptions by enabling the overflow, the divide-by-zero, and the invalid floating-point exceptions. The program will print an error message and abort if any of these exceptions occurs. If a floating underflow occurs, the result is set to zero and execution continues. This is called flush-to-zero. This option sets -fp-speculation=strict (Linux and Mac OS X) or /Qfp-speculation:strict (Windows) if no specific -fp-speculation or /Qfp-speculation option is specified. The -fpe0 or /fpe:0 option sets -ftz (Linux and Mac OS X) /Qftz(Windows). To get more detailed location information about where the exception occurred, use -traceback (Linux and Mac OS X) or /traceback (Windows).
On systems based on the IA-32 and Intel® 64 architectures , explicitly setting -fpe0 or /fpe:0 can degrade performance since the generated code stream must be synchronized after each floating-point instruction to allow for abrupt underflow fix-up.
-fpe1 or /fpe:1 restricts only floating-point underflow. Floating-point overflow, floating-point divide-by-zero, and floating-point invalid produce exceptional values (NaN and signed Infinities) and execution continues. If a floating-point underflow occurs, the result is set to zero and execution continues. The /fpe:1 option sets -ftz or /Qftz.
On systems based on the IA-32 and Intel® 64 architectures , explicitly setting -fpe1 or /fpe:1 can degrade performance since the generated code stream must be synchronized after each floating-point instruction to allow for abrupt underflow fix-up.
-fpe3 or /fpe:3 is the default on all processors, which allows full floating-point exception behavior. Floating-point overflow, floating-point divide-by-zero, and floating-point invalid produce exceptional values (NaN and signed Infinities) and execution continues. Floating underflow is gradual: denormalized values are produced until the result becomes 0.
The -fpe or /fpe option enables exceptions in the Fortran main program only. The floating-point exception behavior set by the Fortran main program remains in effect throughout the execution of the entire program unless changed by the programmer. If the main program is not Fortran, the user can use the Fortran intrinsic FOR_SET_FPE to set the floating-point exception behavior.
When compiling different routines in a program separately, you should use the same value of n in -fpen or /fpe:n.
An example follows:
IMPLICIT NONE
real*4 res_uflow, res_oflow
real*4 res_dbyz, res_inv
real*4 small, big, zero, scale
small = 1.0e-30
big = 1.0e30
zero = 0.0
scale = 1.0e-10
! IEEE underflow condition (Underflow Raised)
res_uflow = small * scale
write(6,100)"Underflow: ",small, " *", scale, " = ", res_uflow
! IEEE overflow condition (Overflow Raised)
res_oflow = big * big
write(6,100)"Overflow:", big, " *", big, " = ", res_oflow
! IEEE divide-by-zero condition (Divide by Zero Raised)
res_dbyz = -big / zero
write(6,100)"Div-by-zero:", -big, " /", zero, " = ", res_dbyz
! IEEE invalid condition (Invalid Raised)
res_inv = zero / zero
write(6,100)"Invalid:", zero, " /", zero, " = ", res_inv
100 format(A14,E8.1,A2,E8.1,A2,E10.1)
end
Consider the following command line:
ifort fpe.f90 -fpe0 -fp-model strict -g (Linux and Mac OS X)
ifort fpe.f90 /fpe:0 /fp:strict /traceback (Windows)
Output similar to the following should result:
Windows:
Underflow: 0.1E-29 * 0.1E-09 = 0.0E+00
forrtl: error (72): floating overflow
Image PC Routine Line Source
fpe.exe 0040115B Unknown Unknown Unknown
fpe.exe 0044DFC0 Unknown Unknown Unknown
fpe.exe 00433277 Unknown Unknown Unknown
kernel32.dll 7C816D4F Unknown Unknown Unknown
Linux and Mac OS X:
./a.out
Underflow: 0.1E-29* 0.1E-09 = 0.0E+00
forrtl: error (72): floating overflow
Image PC Routine Line Source
a.out 0804A063 Unknown Unknown Unknown
a.out 08049E78 Unknown Unknown Unknown
Unknown B746B748 Unknown Unknown Unknown
a.out 08049D31 Unknown Unknown Unknown
Aborted
The following command line uses /fpe1:
ifort fpe.f90 -fpe1 -g (Linux and Mac OS X)
ifort fpe.f90 /fpe:1 /traceback (Windows)
The following output is produced:
Underflow: 0.1E-29 * 0.1E-09 = 0.0E+00
Overflow: 0.1E+31 * 0.1E+31 = Infinity
Div-by-zero: -0.1E+31 / 0.0E+00 = -Infinity
Invalid: 0.0E+00 / 0.0E+00 = NaN
The following command line uses /fpe3:
ifort fpe.f90 -fpe3 -g (Linux and Mac OS X)
ifort fpe.f90 /fpe:3 /traceback (Windows)
The following output is produced:
Underflow: 0.1E-29 * 0.1E-09 = 0.1E-39
Overflow: 0.1E+31 * 0.1E+31 = Infinity
Div-by-zero: -0.1E+31 / 0.0E+00 = -Infinity
Invalid: 0.0E+00 / 0.0E+00 = NaN