Visual Studio C ++ 2008/2010 - 打破浮动NaN [英] Visual Studio C++ 2008 / 2010 - break on float NaN

查看:328
本文介绍了Visual Studio C ++ 2008/2010 - 打破浮动NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法设置Visual Studio(只是从2008到2010年升级)破解,如果一个断言失败,每当任何浮点数变成NaN,QNAN,INF等等?

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?

到目前为止,我刚刚使用了assert(x == x)技巧,但我宁愿隐式,所以我不必添加断言无处不在。

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.

很奇怪,我通过google找不到这个答案。一些关于浮点异常的东西,但我不知道他们是否是相同的东西,我已经尝试启用他们在Visual Studio中,但程序不会打破,直到发生灾难性的事情,因为NaN后

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.

推荐答案

1)转到项目选项并启用 / fp:strict

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

2)使用 _controlfp 设置浮点控制字(见下面的代码)。

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;
}

这篇关于Visual Studio C ++ 2008/2010 - 打破浮动NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆