Visual Studio C++ 2008 / 2010 - break on float NaN

后端 未结 4 1601
一向
一向 2020-12-13 19:02

Is there any way to set up Visual Studio (just upgraded from 2008 to 2010) to break, as if an assertion failed, whenever any floating point number becomes NaN, QNAN

相关标签:
4条回答
  • 2020-12-13 19:36

    1) Go to project option and enable /fp:strict (C/C++ -> Code Generation -> Floating Pint Model).

    2) Use _controlfp to set the floating-point control word (see code below).

    #include <float.h>
    unsigned int fp_control_state = _controlfp(_EM_INEXACT, _MCW_EM);
    
    #include <math.h>
    
    int main () {
    
        sqrtf(-1.0);    // floating point exception
    
        double x = 0.0;
        double y = 1.0/x;   // floating point exception
    
        return 0;
    }
    
    0 讨论(0)
  • At least on x86, when you generate an NaN etc, one of the FPU status register bits is set. There's a way you can set so that it throws a H/W exception on the next subsequent FP operation occurs, but that's not quite as soon as you hoped for. I can't recall the reference though.

    0 讨论(0)
  • 2020-12-13 19:45

    Try enabling fp exceptions

    0 讨论(0)
  • 2020-12-13 19:45

    I am not sure if this is possible the way you want it, but You could create an macro which wraps the code in the marked line into an assert or which sets a breakpoint for this.

    Hope this helps

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