17

I have an C++ program. Somewhere in the program (hard to reproduce, but reproduceable) a caclculation results in a float beeing set to a NaN. Since a floating point operation involving a NaN results in a NaN, this spreads fast.

Is there any way I can setup the compiler (gcc 4.4) or the debuger (gdb) to stop when a floating point operation results in a NaN? That would be extremely useful.

Thanks! Nathan

PS: It might matter: I am working under ubuntu linux 10.10.

Nathan
  • 7,099
  • 14
  • 61
  • 125

1 Answers1

19

You could enable floating point exceptions - see glibc Control Functions - then you'll get a SIGFPE when your NaN value is produced

Erik
  • 88,732
  • 13
  • 198
  • 189
  • 13
    feenableexcept(FE_INVALID | FE_OVERFLOW); Does the trick, thank you! – Nathan Mar 22 '11 at 16:05
  • 1
    The answer is correct. However on OSX you need something like this http://www-personal.umich.edu/~williams/archive/computation/fe-handling-example.c because feenableexcept() is not available. – fferri Dec 12 '14 at 21:51
  • dont forget to add #include while using feenableexcept() – Han Sep 03 '20 at 08:12