Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug C++ code: Catch first NaN appearance [duplicate]

Tags:

c++

debugging

nan

Is there a simple way to check a C++ code in a debugger for the very first appearance of a NaN value?

like image 300
Thomas W. Avatar asked Aug 13 '12 12:08

Thomas W.


People also ask

What causes NaN in C?

NaN, an acronym for Not a Number is an exception that usually occurs in the cases when an expression results in a number that is undefined or can't be represented. It is used for floating-point operations. For example: The square root of negative numbers.

What does step over do in debugging?

Stepping Over a Function on the Debug toolbar or select Debug:Step Over. The debugger executes the function call and then pauses after the function returns. In addition, you can user hotkey: Alt+Num* quickly locate to the current execution point.


2 Answers

The answer is given here: https://stackoverflow.com/a/5394095/1326595

Just include

#include <fenv.h>

and than add the following line to the code:

feenableexcept(FE_INVALID | FE_OVERFLOW);

The debugger is than able to capture the signal and shows the very first occurrence of a NaN.

like image 177
Thomas W. Avatar answered Sep 30 '22 18:09

Thomas W.


By IEEE standard the following condition is false for NaN's:

val == val

and you could use it to trigger assert or software breakpoint, but beware of compiler optimizations. Probably, in debug build it would not get optimized away

like image 29
Roman Saveljev Avatar answered Sep 30 '22 18:09

Roman Saveljev