-ffpe-trap and floating-point exceptions

IEEE arithmetic is designed not to stop. A division by zero gives an infinity, zero divided by zero gives a NaN, an overflow gives an infinity again, and the program carries the value on until a result looks strange. That is the right behaviour for a library and the wrong one for a test, so gfortran can turn the processor's exception flags into signals: -ffpe-trap stops the program at the instruction that raised the exception, and with -g the backtrace names the line.

A division by zero that does not stop

The program divides one by zero and then multiplies the result by zero. Both are IEEE operations with a defined result, and neither is an error as far as the language is concerned:

The file fp.f90:

program fp implicit none real :: x, y, z x = 1.0 y = 0.0 z = x / y print '(a,es12.3)', 'z = ', z print '(a,es12.3)', 'z * 0 = ', z * 0.0 end program fp

gfortran -Wall -Wextra -std=f2018 fp.f90 -o fp
./fp

z =     Infinity
z * 0 =         NaN

The status is 0 and no message is printed. That is not a compiler oversight: Fortran 2018 says an operation that raises an IEEE exception gives the value the standard defines, and a program that wants to know about it either tests the value or uses the IEEE modules of lesson 089. The infinity and the NaN here are the two values 090 put through their paces.

-ffpe-trap=zero

-ffpe-trap=zero asks the runtime to trap the divide-by-zero exception, and the same source stops at the division. A test build adds -g, because the message itself does not say where the fault is:

gfortran -Wall -Wextra -std=f2018 -g -ffpe-trap=zero fp.f90 -o fp_trap
./fp_trap


Program received signal SIGFPE: Floating-point exception - erroneous arithmetic operation.

Backtrace for this error:
#0  0x7ea395b65430 in ???
#1  0x7ea395b64345 in ???
#2  0x7ea395f0888a in ???
  at src/signal/x86_64/restore.s:1
#3  0x634c693be233 in fp
  at /tmp/w115/fp.f90:7
#4  0x634c693be3f2 in main
  at /tmp/w115/fp.f90:10

Frame 3 is the division at line 7. The shell reports status 136, which is 128 plus the number of SIGFPE, and a test runner that only looks at the status sees a failure. The same run without -g prints the same five lines and a list of addresses with no procedure names, so the flag is not optional in practice.

Note what the message says and what it does not. It says SIGFPE and erroneous arithmetic operation for every trap of this kind; the name of the exception (zero, invalid, overflow) is in the flag the program was built with, not in the output. That is the difference from -fcheck, which prints a sentence about the subscript it rejected.

Invalid operations and NaN

Zero divided by zero is not a division by zero: the exception is invalid operation, and -ffpe-trap=zero does not catch it. This program shows the value and then asks the IEEE module what it is:

The file invalid2.f90:

program invalid2 use, intrinsic :: ieee_arithmetic implicit none real :: a, b, c a = 0.0 b = 0.0 c = a / b print '(a,es12.3)', 'c = ', c print '(a,l1)', 'ieee_is_nan(c) = ', ieee_is_nan(c) end program invalid2

gfortran -Wall -Wextra -std=f2018 invalid2.f90 -o invalid2
./invalid2

c =          NaN
ieee_is_nan(c) = T

Built with -ffpe-trap=invalid the division stops the program, and the backtrace names line 8, the c = a / b of the source above:

gfortran -Wall -Wextra -std=f2018 -g -ffpe-trap=invalid invalid2.f90 -o invalid2_trap
./invalid2_trap


Program received signal SIGFPE: Floating-point exception - erroneous arithmetic operation.

Backtrace for this error:
#0  0x71a63b067430 in ???
#1  0x71a63b066345 in ???
#2  0x71a63b40a88a in ???
  at src/signal/x86_64/restore.s:1
#3  0x5c16e04eb26b in invalid2
  at /tmp/w115/invalid2.f90:8
#4  0x5c16e04eb434 in main
  at /tmp/w115/invalid2.f90:2

A program that is tested for numerical correctness usually sets the two traps together, -ffpe-trap=invalid,zero,overflow, because all three produce a value that looks like a number and is not one. The build with -ffpe-trap=zero alone runs this program to the end and prints the NaN, which is exactly the failure a test suite is meant to notice.

Overflow

The largest single-precision real is 3.403E+38, and doubling it overflows to an infinity. That is the third trap:

The file over.f90:

program over implicit none real :: big big = huge(1.0) print '(a,es12.3)', 'big = ', big big = big * 2.0 print '(a,es12.3)', 'big = ', big end program over

gfortran -Wall -Wextra -std=f2018 over.f90 -o over
./over

big =    3.403E+38
big =     Infinity

With -ffpe-trap=overflow the first line is printed and the program stops on the multiplication at line 7, before the second line. The trap fires on the operation, not on the print, so a lesson about a value that went out of range starts from the line the backtrace gives.

Uninitialised values

The trap finds one more class of bug, with a flag of its own. An accumulator that is never set to zero starts from whatever was in its memory, and here that word happened to be zero, so the program is right by luck:

The file uninit.f90:

program uninit implicit none real :: total, x(3) integer :: i x = [1.0, 2.0, 3.0] do i = 1, 3 total = total + x(i) end do print '(a,f8.3)', 'total = ', total end program uninit

gfortran -Wall -Wextra -std=f2018 uninit.f90 -o uninit
./uninit

total =   6.000

-finit-real=snan initialises every local real to a signalling NaN, and -ffpe-trap=invalid turns the first use of it into a signal. The same source then fails at the addition on line 8, which is the line that read the uninitialised value:

gfortran -Wall -Wextra -std=f2018 -g -finit-real=snan -ffpe-trap=invalid uninit.f90 -o uninit_trap
./uninit_trap


Program received signal SIGFPE: Floating-point exception - erroneous arithmetic operation.

Backtrace for this error:
#0  0x753018ffd430 in ???
#1  0x753018ffc345 in ???
#2  0x7530193a088a in ???
  at src/signal/x86_64/restore.s:1
#3  0x5819bb34b297 in uninit
  at /tmp/w115/uninit.f90:8
#4  0x5819bb34b3a5 in main
  at /tmp/w115/uninit.f90:11

-finit-real also accepts zero, inf and nan; the signalling version is the one that makes the trap fire, because the quiet NaN of nan spreads through the arithmetic without raising anything. Integers get -finit-integer with a value, which is exact rather than a trap: a large negative sentinel such as -999999 shows up in the output.

Rules and limits

Add -ffpe-trap=invalid,zero,overflow to the test build of the previous lesson: -g -fcheck=all -finit-real=snan -ffpe-trap=invalid,zero,overflow. Keep the release build without the traps, because a trapped program stops and a library that is expected to return an infinity does not want to. The traps are a property of the build, not of the source, and the same code runs both ways.

Three limits are worth knowing. The trap fires on the machine instruction, so at -O2 the compiler may move a floating-point expression to a place that is not the line a reader expects, or remove it when the result is unused. A trap in a program built with -ffast-math is meaningless, because that option tells the compiler to assume the exceptions do not happen. And the trap stops the process: it is not an exception a program can catch and continue from, unlike the IEEE flags of lesson 089, which a program can read and clear after the fact.

Exercises

  1. Build fp.f90 with -ffpe-trap=zero,overflow and run it. Which line does the backtrace name, and why not the multiplication?
  2. Change fp.f90 so that y is read from the command line and run it with 0 and with 2. What changes?
  3. Build uninit.f90 with -finit-real=inf and -ffpe-trap=overflow. What does the first addition do?
  4. Build invalid2.f90 with -ffpe-trap=zero,overflow. Does the program stop? Which value does it print?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does Fortran do with a division by zero by default?

    Answer

    It produces an infinity and the program goes on

    IEEE arithmetic defines the result.

  2. What does -ffpe-trap=zero do?

    Answer

    Turns the divide-by-zero exception into a signal that stops the program

    The program receives SIGFPE.

  3. Which flag makes the backtrace name the source line of the trapped operation?

    Answer

    -g

    Without it the frames are addresses.

  4. Which exception does 0.0 / 0.0 raise?

    Answer

    Invalid operation

    -ffpe-trap=invalid catches it, -ffpe-trap=zero does not.

  5. What is the status the shell reports for a program stopped by SIGFPE?

    Answer

    136

    128 plus 8, the number of SIGFPE.

  6. What does -finit-real=snan do?

    Answer

    Initialises local reals to a signalling NaN

    The first use raises invalid when the trap is on.

  7. Why is -finit-real=nan weaker than snan for this purpose?

    Answer

    A quiet NaN spreads through arithmetic without raising an exception

    Only the signalling NaN raises the flag.

  8. Why are the traps left out of the release build?

    Answer

    A release program stops where a library would return an infinity

    The traps are for finding bugs, not for production behaviour.

  9. What does the trap message itself say about the exception?

    Answer

    Only SIGFPE and "erroneous arithmetic operation"

    The line comes from the backtrace, the kind from the flag.

  10. Which option makes a trap meaningless?

    Answer

    -ffast-math

    It tells the compiler to assume the exceptions do not happen.

The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev