NaN, infinity and IEEE values
Two values of IEEE floating point are not numbers in the ordinary sense: an infinity, which a division by zero produces, and a NaN, which a calculation that has no answer produces. Both are values a program can hold, print and pass to a procedure, and both break the arithmetic a reader assumes. This lesson builds them without any input, shows which comparisons survive a NaN, and reads the error of asking an integer whether it is one.
Infinity
one / zero with both in variables is a division by zero, which the hardware completes with the largest value it can represent in the sense of "beyond every finite number": the infinity. Adding a finite value to it changes nothing, and subtracting it from itself has no answer, which is the NaN of the next section.
The file nan.f90:
program nandemo
use, intrinsic :: ieee_arithmetic
implicit none
real :: zero, one, nan, inf, big
zero = 0.0
one = 1.0
inf = one / zero
nan = inf - inf
print '(a,f8.1)', 'one / zero = ', inf
print '(a,f8.1)', 'inf + 1 = ', inf + one
print '(a,f8.1)', 'inf - inf = ', nan
print '(a,l1)', 'ieee_is_nan(nan) = ', ieee_is_nan(nan)
print '(a,l1)', 'nan == nan = ', nan == nan
print '(a,l1)', 'nan /= nan = ', nan /= nan
print '(a,l1)', 'nan < 1.0 = ', nan < one
print '(a,l1)', 'nan > 1.0 = ', nan > one
big = huge(one)
print '(a,es15.7)', 'huge = ', big
print '(a,es15.7)', 'tiny = ', tiny(one)
print '(a,l1)', 'big + big is finite = ', ieee_is_finite(big + big)
end program nandemo
The build warns about the two comparisons with a real, which is gfortran being helpful: equality of two reals is almost never what a program wants, and a NaN is the extreme case of why.
gfortran -Wall -Wextra -std=f2018 nan.f90 -o nan
nan.f90:15:40:
15 | print '(a,l1)', 'nan == nan = ', nan == nan
| 1
Warning: Equality comparison for REAL(4) at (1) [-Wcompare-reals]
nan.f90:16:40:
16 | print '(a,l1)', 'nan /= nan = ', nan /= nan
| 1
Warning: Inequality comparison for REAL(4) at (1) [-Wcompare-reals]
./nan
one / zero = Infinity
inf + 1 = Infinity
inf - inf = NaN
ieee_is_nan(nan) = T
nan == nan = F
nan /= nan = T
nan < 1.0 = F
nan > 1.0 = F
huge = 3.4028235E+38
tiny = 1.1754944E-38
big + big is finite = F
NaN
inf - inf is not a number, and the IEEE model gives that result its own value: the NaN, for not a number. Every arithmetic operation with a NaN in it produces a NaN, so a single bad value at the start of a computation makes every result downstream bad, and a program that prints a column of NaNs has one bug rather than a hundred.
The literal in a source file cannot be written as 0.0/0.0 without a compile-time warning, which is why the program builds one from variables. ieee_value(x, ieee_quiet_nan) of the previous lesson is the direct route and needs the module; a subtraction of two infinities is the arithmetic one.
Comparisons with a NaN
The five lines of comparisons are the part a reader has to memorise. A NaN is not equal to anything, not even to itself; it is not less than anything and not greater than anything. The one comparison that is true is nan /= nan, which is how old code detected a NaN before the IEEE modules existed: if (x /= x) then is true exactly when x is a NaN.
Two consequences follow. A sort or a search that compares reals puts a NaN nowhere in particular, because the comparison never says which side it belongs to. And a maximum by comparison, max(a, b), may return either argument when one of them is a NaN, since neither order is right. A numerical program that can meet a NaN has to test for it before it compares.
Detecting it
ieee_is_nan is the test to write: it is a predicate about the value, not a comparison, and it answers the question the comparisons cannot. ieee_is_finite is its companion, true for every value that is neither a NaN nor an infinity, and it is the test to use on a result that came from data.
Both are in ieee_arithmetic, both take a real and answer a logical, and both work for single and double precision. A program that reads a table of reals from a file and finds one of them NaN has either a bad record or a division that produced it, and either way the test belongs where the value enters the program rather than where it is printed.
How the values print
Infinity is nine characters, which is why the format in the program is f8.1 and the output still lines up: gfortran widens the field rather than truncating the word. NaN is three characters and is right-aligned in the same field. Neither is a number a program should parse back: a file format that may contain them needs a convention of its own.
The last two lines are the finite limits of the kind. huge(one) is the largest finite single-precision value and tiny(one) the smallest positive normal one, both as es15.7 shows. Twice huge overflows to an infinity, and ieee_is_finite says so, which is the honest test for an overflow rather than a comparison against huge written out by hand.
An integer to ieee_is_nan
The IEEE enquiries are for real and complex arguments; an integer has no NaN and no infinity, so the generic has no specific for it:
program badarg
use, intrinsic :: ieee_arithmetic
implicit none
integer :: i
logical :: f
i = 3
f = ieee_is_nan(i)
print *, f
end program badarg
gfortran -Wall -Wextra -std=f2018 badarg.f90 -o badarg
badarg.f90:8:6:
8 | f = ieee_is_nan(i)
| 1
Error: There is no specific function for the generic 'ieee_is_nan' at (1)
The message is about the generic rather than the argument, which is how an interface reports a call that matched no specific. Converting the integer to a real first would compile, and it would answer a different question, since an integer can never be a NaN.
Rules for a numerical program
Three rules keep these values from spreading. Test input at the boundary with ieee_is_finite and refuse a record that fails. Never compare two reals for equality in a branch that is not about exactness, and never sort or take a maximum without deciding what a NaN does. And build the program with -ffpe-trap in a test run, so that a division by zero stops at its line instead of producing an infinity that travels.
The next lesson is the third rule in detail: what a tolerance is, how to choose one, and why an absolute comparison of two reals is almost always a bug even when neither is a NaN.
Exercises
- Print nan + 1.0, nan * 0.0 and nan / nan. Which of them is a NaN, and which is a surprise?
- Compute max(1.0, nan) and max(nan, 1.0). Are the two answers the same?
- Print 1.0 / tiny(one) and tiny(one) / 2.0. Is the second a denormal, a zero or an infinity, and what does ieee_is_finite say?
- Sort an array that contains one NaN with the routine of lesson 044 and print the order. Where did the NaN go, and why?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is 1.0 / 0.0 in IEEE arithmetic with the flags masked?
Answer
An infinity of the sign of the operands
The division by zero completes with an infinity and sets a flag.
-
What does inf - inf produce?
Answer
A NaN
The subtraction has no defined result.
-
What does this program print?
program p use, intrinsic :: ieee_arithmetic implicit none real :: x x = ieee_value(x, ieee_quiet_nan) print *, x == x, x /= x end program pAnswer
F T
A NaN equals nothing, including itself.
-
Which comparison is true for a NaN?
Answer
x /= x
It is the classic idiom and it works only for NaN.
-
What does
ieee_is_finite(x)answer for an infinity?Answer
.false.
Only finite numbers are finite.
-
Why can a program not write
0.0/0.0as a NaN literal?Answer
The compiler folds the constant and warns about the division
Build the value from variables or use ieee_value.
-
What does a sort do with a NaN in the array?
Answer
Nothing defined: the comparisons are all false, so its place is arbitrary
A comparison that is never true gives the algorithm no order.
-
What is the result of any arithmetic on a NaN?
Answer
A NaN
The value spreads through every operation.
-
What does gfortran report for
ieee_is_nan(i)with an integer i?Answer
Error: there is no specific function for the generic
An integer has no NaN, so the generic has no specific.
-
How wide is the text
Infinityin a formatted output?Answer
Nine characters; gfortran widens a narrow field rather than truncating
The run shows it in an f8.1 field and the line still lines up.
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