C++浮点数转 nan
我想知道 C++ 中浮点数 nan 的含义是什么。我正在使用一个很大的数据集,而且很难追踪。我想知道如何将浮点数更改为 nan 以减少错误可能性。
我找到了导致nan问题的代码。我发现 s/m 在某些情况下是 nan 。但我不知道如何解决。
float gp(float x){
float e = 2.71828183;
x *= -1;
float s = pow(e,x);
float m = (1 + pow(e,x)) * (1 + pow(e,x));
return s / m;}
I want to know what makes a float number nan in c++. I am using a large dataset and it is really hard to trace. I want to know the ways of changing a float number to nan to reduce bug possibilities.
I found the code that causes the nan problem. I found that s/m is nan in some cases. But I don't know how to solve it.
float gp(float x){
float e = 2.71828183;
x *= -1;
float s = pow(e,x);
float m = (1 + pow(e,x)) * (1 + pow(e,x));
return s / m;}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
摘自 维基百科 ->特殊值-> nan
看看你的代码:无穷大乘以 0 是可能的,是吗?
编辑:
0 <= s <= +inf
1 <= m <= +inf
s / m:
我认为这是唯一产生 NaN 的东西。
Taken from wikipedia -> special values -> nan
Looking at you code: infinity times 0 is possible, is it?
edit:
0 <= s <= +inf
1 <= m <= +inf
s / m:
I think that's the only thing that makes a NaN.
如果您可以将
x
保持在 0 和FLT_MAX
之间(在我的例子中为 3.40E+38),您的 gp 函数将不会返回 NaN。If you can keep
x
between 0 andFLT_MAX
(3.40E+38 in my case), your gp function will not return NaN.您在评论中说您只使用
*
、+
、-
。[编辑:您后来说过您还使用了 pow 和除法,这引入了一些获得 NaN 的额外方法。例如,如果参数 x 是一个很大的负值,那么 pow(e,-x) 就是无穷大,因此您可以轻松地最终计算无穷大/无穷大,这是另一个NaN]
因此,如果您有 IEEE 浮点,则假设此摘要是正确的,生成 NaN 的唯一方法是:
或:
因此,如果您检查并捕获无穷大,您也不必担心 NaN。也就是说,通常的方法是让这些值作为安静的 NaN 传播,并在最后进行检查。
对于使用非 IEEE 算术的 C++ 实现,我不确定允许 NaN 时的规则是什么。我可以在标准中查找它们,但你也可以;-)
You say in a comment that you only use
*
,+
,-
.[Edit: you've since said that you also use
pow
and division, which introduce some extra ways to get NaN. For example if the parameterx
is a large negative value thenpow(e,-x)
is infinity, so you can easily end up computing infinity/infinity, which is another NaN]So, if you have IEEE floating-point then assuming this summary is correct, the only ways you can generate NaN are:
or:
So if you check for and catch infinities, you don't have to worry about NaNs as well. That said, the usual way is to let such values propagate as quiet NaNs, and check at the end.
For C++ implementations using non-IEEE arithmetic, I'm not sure what the rules are when a NaN is permitted. I could look them up in the standard, but then again so could you ;-)
例如,给你 NaN。
http://www.gnu.org/s/libc /manual/html_node/Infinity-and-NaN.html
give you NaN, for example.
http://www.gnu.org/s/libc/manual/html_node/Infinity-and-NaN.html
编辑尝试使用
double
而不是float
。可能取决于您使用的编译器,但一般选项是:
EDIT Try use
double
instead offloat
.Probably it depends to compiler you using but general option is:
这正是启用和捕获浮点异常的用例。这样您就可以准确检测 NaN(或其他异常值)首次出现的位置。
但是,这是一个依赖于平台的功能,因此您可能需要查看编译器和/或硬件的文档。
This is exactly the use case for enabling and trapping floating-point exceptions. That way you can detect exactly where the NaN (or other exception value) first appears.
However, that's a platform-dependant feature, so you may have to look into the documentation of your compiler and/or hardware.