【发布时间】:2018-09-03 04:02:32
【问题描述】:
g++ 版本 (5.4.0) 为 std::exp 返回 NaN,浮点数小于大约 87。
但是docs to std::exp
建议,它接近0 对于小数字:
如果参数是-∞,则返回+0
这是标准库实现中的错误,还是我缺少什么?
效果可以是reproduced这样的:
#include <cmath>
#include <fenv.h>
#include <iostream>
int main()
{
feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT);
for (float x = 30; x > -1000; --x)
{
float y = std::exp(x);
std::cout << x << "\t\t" << y << std::endl;
}
}
输出:
30 1.06865e+13
29 3.93133e+12
28 1.44626e+12
27 5.32048e+11
...
-84 3.3057e-37
-85 1.2161e-37
-86 4.47378e-38
-87 1.64581e-38
Floating point exception
【问题讨论】:
-
在您的示例中触发的 feexp 可能是一些下溢异常,顺便说一句。不过,这不会产生 NaN,而是先产生次正规数,然后产生 0。
-
如果你使用了
double重载,它会在-708 之后导致下溢。或者 -708.4 表示 IEEE 兼容的double准确地说。
标签: c++ gcc floating-point g++ nan