【发布时间】:2014-02-04 17:16:57
【问题描述】:
我试图了解以下程序的奇怪行为。显然,在定义全局变量“bug”的过程中发生了溢出,但是程序在1.0+2.0的无辜计算过程中抛出了浮点异常。
#include <iostream>
#include <cmath>
#include <fenv.h>
using namespace std;
const double bug = pow(10.0,pow(10.0,10.0));
int main(void)
{
feenableexcept(-1);
cout << "before" << endl;
cout << 1.0 + 2.0 << endl;
cout << "after" << endl;
return 0;
}
我尝试使用 g++ 和 clang++ 编译它,但两者的输出相同
before
Floating point exception
【问题讨论】:
-
我不确定,但可能是因为在您启用它们时设置了异常标志,但在您执行添加之前不会对其进行测试。