should divide by zero raise an exception

≯℡__Kan透↙ 提交于 2020-02-06 07:51:32

问题


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.'

来源:https://stackoverflow.com/questions/52612895/should-divide-by-zero-raise-an-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!