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

4

27

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, INF, etc?

Up until now I have just been using the assert(x == x) trick, but I would rather something implicit, so that I dont have to add assertions everywhere.

Quite surprised I can't find an answer to this via google. Some stuff about 'floating point exceptions', but I'm not sure if they are the same thing, and I've tried enabling them in Visual Studio, but the program doesn't break until something catastrophic happens because of the NaN later on in execution.

Eu answered 15/12, 2010 at 20:31 Comment(0)
K
32

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;
}
Knickerbocker answered 15/12, 2010 at 21:38 Comment(2)
Actually you are right. You are unmasking every exception except _EM_INEXACT, so _EM_INVALID will be triggered.Onagraceous
The fp control word is a per thread state. To get the thing working in a multithreaded environment call _controlfp once per thread. Another issue for me was that I needed to set C/C++ -> Code Generation -> Modify the Enable C++ Exceptions to "Yes With SEH Exceptions" to get any break.Mastery
E
4

Try enabling fp exceptions

Ewers answered 15/12, 2010 at 20:45 Comment(1)
OK, added _clearfp and _controlfp commands, as well as the Visual Studio menu option. Still not getting exceptions, though, but I think this is a seperate problem since the values are QNAN, which it seems don't raise exceptions. I'll start a new question related to this.Eu
U
1

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.

Unconformity answered 15/12, 2010 at 20:36 Comment(2)
Trying to find a way to do this. All I can find is the option in Visual Studio under "Code Generation" for floating point exceptions. However, enabling exceptions doesn't have any effect.Eu
See watson1180's answer and this: #2770314Potheen
V
0

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

Villiform answered 15/12, 2010 at 20:35 Comment(1)
I'm not sure what you mean by this. It sounds like you mean to add assert statements but wrap it in a macro? How does that help?Eu

© 2022 - 2024 — McMap. All rights reserved.