gpt4 book ai didi

c++ - C++ 中的 SIGFPE 误导

转载 作者:行者123 更新时间:2023-11-28 07:07:33 25 4
gpt4 key购买 nike

我试图理解以下程序的奇怪行为。很明显,在全局变量“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

最佳答案

const double bug = pow(10.0,pow(10.0,10.0)); 应该被使用。因为 pow 需要 (double,double) 参数,而你传递的是 (int,int)

关于c++ - C++ 中的 SIGFPE 误导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21546484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com