How to trace a NaN in C++

后端 未结 4 432
生来不讨喜
生来不讨喜 2020-12-05 00:17

I am going to do some math calculations using C++ . The input floating point number is a valid number, but after the calculations, the resulting value is NaN. I would like

相关标签:
4条回答
  • 2020-12-05 01:08

    Some notes on floating point programming can be found on http://ds9a.nl/fp/ including the difference between 1/0 and 1.0/0 etc, and what a NaN is and how it acts.

    0 讨论(0)
  • 2020-12-05 01:11

    In Visual Studio you can use the _controlfp function to set the behavior of floating-point calculations (see http://msdn.microsoft.com/en-us/library/e9b52ceh(VS.80).aspx). Maybe there is a similar variant for your platform.

    0 讨论(0)
  • 2020-12-05 01:14

    One can enable so-called "signaling NaN". That should make it easily possible to make the debugger find the correct position.

    Via google, I found this for enabling signaling NaNs in C++, no idea if it works:

    std::numeric_limits::signaling_NaN();

    Usefulness of signaling NaN?

    0 讨论(0)
  • 2020-12-05 01:17

    Since you mention using gdb, here's a solution that works with gcc -- you want the functions defined in fenv.h :

    #define _GNU_SOURCE
    #include <fenv.h>
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
       double dirty = 0.0;
    
       feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT);  // Enable all floating point exceptions but FE_INEXACT
       double nanval=0.0/dirty;
       printf("Succeeded! dirty=%lf, nanval=%lf\n",dirty,nanval);
    }
    

    Running the above program produces the output "Floating point exception". Without the call to feenableexcept, the "Succeeded!" message is printed.

    If you were to write a signal handler for SIGFPE, that might be a good place to set a breakpoint and get the traceback you want. (Disclaimer: haven't tried it!)

    0 讨论(0)
提交回复
热议问题