使用std::exp来计算e^-infinity收益-infinity使用无限的浮点表示和使用Visual C++ 2013年我希望它返回0,这是发生了什么的Win32版本,或版本建立一个64二进制文件时std::exp,需要一个double.
以下代码(构建为x64)演示了此问题.
#include <limits>
#include <iostream>
int main(const int argc, const char** argv) {
std::cout << "exp of float -infinity: " << std::exp(-std::numeric_limits<float>::infinity()) << std::endl;
std::cout << "exp of double -infinity: " << std::exp(-std::numeric_limits<double>::infinity()) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
用于编译的命令行选项(取自Visual Studio):
/GS /Wall /Gy /Zc:wchar_t /Zi /Gm- /Od /sdl /Fd"x64\Release\vc120.pdb" /fp:precise /D "_MBCS" /errorReport:prompt /WX /Zc:forScope /Gd /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Fp"x64\Release\NumericLimitsTest.pch"
Run Code Online (Sandbox Code Playgroud)
以上产出:
exp of float -infinity: -1.#INF …Run Code Online (Sandbox Code Playgroud)