dgu*_*uan 18 c++ floating-point nan
在需要处理sin(x)/ x函数的程序中,我遇到了NAN问题,我简化了以下代码中的问题:
#include <iostream>
#include <cmath>
int main()
{
std::cout.precision(15);
//This line compiles and run in g++, but does not compile in Visual Studio 2013
std::cout << 0.0/0.0 << std::endl;
//This line compiles and run in both g++ and VS2013
std::cout << std::sin(0.0)/0.0 << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在g ++中,输出为:-nan -nan,在VS2013中,输出为:-1.IND,因为第一行没有编译所以我把它注释掉了.
我的问题是:
这个'-1.IND'是什么意思?
似乎NAN处理依赖于编译器,这应该在C++中标准化吗?为什么?
我用这个hack来处理这个问题:
double sinc(double x)
{
if(x == 0.0)
return 1.0;
return std::sin(x)/x;
}
Run Code Online (Sandbox Code Playgroud)这是正确的方法吗?
编辑:另一个问题,4.为什么VS2013处理0.0/0.0和sin(0.0)/0.0不同?
nas*_*-sh 12
在SO上回答有类似的问题:
1.这个'-1.IND'是什么意思?
请参阅1.#INF00,-1.#IND00和-1.#IND是什么意思?
2.似乎NAN处理依赖于编译器,这应该在C++中标准化吗?为什么?
我用这个黑客来处理这个问题:
Run Code Online (Sandbox Code Playgroud)double sinc(double x) { if(x == 0.0) return 1.0; return std::sin(x)/x; }这是正确的方法吗?
是的,这个sinc函数的实现将起作用(感谢@MSalters的评论)在数学上是正确的; 但是,请记住,虽然它适用于这种情况,但不要养成比较double类型的习惯==.
为(4)添加答案,sin(x)是一个运行时函数,因此sin(0.0)/0.0作为在运行时计算的表达式处理.OTOH,0.0/0.0由编译器完全处理,这就是它发现问题的原因.Visual Studio版本的实现细节,而不是您可以计算的内容.