Use an exception instead of the NaN
16 Aug 2014This is a small notice about a very useful trick. I often have to deal with computations, because the CFD is the part of my activity. And one of the annoying problem in this kind of research is when after many hours of computations you got a Not A Number
result, caused by a stupid mistake. It would be nice, if a computation was interrupted by the NaN.
And there is the solution:
The fenv.h
header declares a set of functions and macros to access the floating-point environment, along with specific types.
According to man:
The feenableexcept() function enable traps for each of the exceptions represented by excepts and return the previous set of enabled exceptions when successful, and -1 otherwise.
Now, what we have without feenableexcept
:
And with: