Quick question: Should comparison with NaN be considered an erroneous arithmetic operation or even lead to a SIGSEGV?
Note that what I would like to ask is the above yes/no question. I hope to hear your opinions about this question. What follows is only explanations about the question. The example below is only to illustrate that the question is not purely imaginary.
Background:
With some (but not all) compilers, it happens in my tests that comparing a number with NaN is sometimes (but not always) caught by -ffpe-trap=invalid and a floating point exception is raised, with “erroneous arithmetic operation” reported.
(Sorry that I do not have a minimal working example, as this behavior appears randomly — comparison with NaN does not always trigger the exception, even with the same compiler and the same options. If this makes the question above meaningless to you, just ignore it.)
Here is a minimal working example:
program test_fpe
use, intrinsic :: ieee_arithmetic, only : ieee_value, ieee_quiet_nan, ieee_is_nan
implicit none
real :: a
a = ieee_value(a, ieee_quiet_nan)
print *, a, ieee_is_nan(a)
print *, a <= 0
end program test_fpe
For the testing results on my side, see my post below. In brief, ifort raises an FPE or SIGSEGV (see Compiler Explorer) on this code, and gfortran raises an FPE (see Compiler Expoler). I just want to confirm whether the results are expected.
Note that it is not my intention to compare numbers with NaN. However, when solving strongly ill-conditioned nonlinear problems, encountering NaN from time to time is not strange. How to avoid this NaN is not my question here.
Thank you.