浮点异常 - 海湾合作委员会的错误? [英] Floating point exceptions - gcc bug?

查看:158
本文介绍了浮点异常 - 海湾合作委员会的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码:

Consider the following code:

#include <fenv.h>
#include <stdio.h>
int main()
{
    #pragma STDC FENV_ACCESS ON
    1.0/0.0;
    printf("%x\n", fetestexcept(FE_ALL_EXCEPT));
}

我希望它打印一个非零值,对应于 FE_DIVBYZERO ,但它打印0.将 main 的第二行更改为 double x = 1.0 / 0.0; code>给出了预期的行为。这是允许的,还是它的一个bug?

I would expect it to print a nonzero value corresponding to FE_DIVBYZERO, but it prints 0. Changing the second line of main to double x = 1.0/0.0; gives the expected behavior. Is this permitted, or is it a bug?

编辑:为什么它是值得的,起初似乎在大多数现实世界代码中,可能会导致fenv异常被引发的操作无法被优化,因此可以安全地执行大型计算并检查是否发生溢出,div-by-zero等。然而,当你考虑内联和优化时,事情会变得混乱,并且出现一个真正的问题。如果这样的函数在由于常量参数而总是最终除以零的情况下被内联,gcc可能会获得真正的智能并优化整个内联函数,本质上为 return INFINITY; 不会引发任何异常。

For what it's worth, at first it may seem that in most real-world code, the operations which might cause fenv exceptions to be raised could not be optimized out, so one could safely perform large computations and check at the end whether any overflow, div-by-zero, etc. happened. However, things get messy and a real issue emerges when you consider inlining and optimization. If such a function got inlined in a situation where it would always end up dividing by zero due to constant arguments, gcc might get really smart and optimize the whole inlined function essentially to return INFINITY; without raising any exceptions.

推荐答案

这是预期的行为。 gcc不会评估表达式,因为之后它与它无关。

This is expected behaviour. gcc doesn't evaluate the expression, because it would have nothing to do with it afterwards.

如果使用-Wall进行编译,它会警告您该语句具有没有效果,并忽略了杂注声明。

If you compile with "-Wall", it warns you that the statement has no effect, and that it ignores the pragma statement.

GCC不完全符合C99。有关详情,请参阅:
http://gcc.gnu.org/c99status.html

GCC is not fully C99 compliant. For more information, see: http://gcc.gnu.org/c99status.html

有关实施此行为的问题,请参阅: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20785

For the issue of implementing this behaviour, see: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20785

这篇关于浮点异常 - 海湾合作委员会的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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