除以零会导致启用浮点异常的异常吗? [英] Should divide by zero cause an exception with floating point exceptions enabled?
问题描述
我一直在VS2015中调试一个C ++应用程序,发现我的一些双变量在除以零后结束了NaN。虽然这是合理的,但我启用了浮点异常(/ fp:except),所以我希望
这可以引发异常。查看
Edit2: 进一步阅读 这里 on
IEE 754告诉我,如果启用了浮点异常,我应该得到一个例外。但是对先前链接的问题的评论却表明 '
名称"浮点异常"是一个历史误称。浮点除零是明确定义的(根据附件F / IEEE754)并且不产生任何信号。 / em>
另外,如果你想使用C ++ try / catch(...)捕获结构化异常,你需要用/ EHa编译 - 参见
https://docs.microsoft.com / en-us / cpp / build / reference / eh-exception-handling-model?view = vs-2017 以及
https://docs.microsoft.com/en-us/cpp/cpp/exception-处理差异?view = vs-2017
I've been debugging a C++ application in VS2015 and found that a number of my double variables were ending up a NaN following a divide by zero. While this is reasonable, I have floating point exceptions enabled (/fp:except) so I would have expected this to raise an exception. Looking at the MS help page, it doesn't list what causes a floating point exception. According to this answer to a related question, divide by zero is a floating point exception. This is not the case, i.e. the following test program with /fp:except enabled
int main()
{
try
{
double x = 1;
double y = 0;
double z = x / y;
printf("%f\n", z);
return 0;
}
catch (...)
{
printf("Exception!\n");
return 0;
}
}
displays "inf". Should this raise a floating point exception?
Edit: Checked the exceptions were enabled in the debugger and get the same result regardless
Edit2: Further reading here on IEE 754 suggests to me that with floating point exceptions enabled I should be getting an exception. A comment to the previously linked question however states 'The name "floating point exception" is a historical misnomer. Floating point division by zero is well-defined (per Annex F/IEEE754) and does not produce any signal.'
Also, if you want to catch a structured exception using C++ try/catch(...) you need to compile with /EHa -- see https://docs.microsoft.com/en-us/cpp/build/reference/eh-exception-handling-model?view=vs-2017 and also https://docs.microsoft.com/en-us/cpp/cpp/exception-handling-differences?view=vs-2017
这篇关于除以零会导致启用浮点异常的异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!