gpt4 book ai didi

c++ - 检查数字是否为 NaN 失败

转载 作者:行者123 更新时间:2023-11-30 03:01:08 25 4
gpt4 key购买 nike

给出以下问题 Divide by Zero PreventionCheck if it's a NaN作为示例,我编写了以下代码:

#include <cstdlib>
#include <iostream>
#include <map>
#include <windows.h>
using namespace std;
bool IsNonNan( float fVal )
{
return( fVal == fVal );
}

int main(int argc, char *argv[])
{
int nQuota = 0;
float fZero = 3 / (float)nQuota;
cout << fZero << endl;
cout << IsNonNan( fZero ) << endl;

system("PAUSE");
return EXIT_SUCCESS;
}

为什么 IsNonNan 返回 true?还有为什么 int nZero = 3/(float)nQuota; 输出:-2147483648

最佳答案

3/0+INF,不是 NaN。试试 0/0

关于c++ - 检查数字是否为 NaN 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11442508/

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