C++浮点数转 nan

发布于 2024-10-26 18:36:43 字数 291 浏览 4 评论 0原文

我想知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

鸢与 2024-11-02 18:36:43

摘自 维基百科 ->特殊值-> nan

  • 0/0
  • ∞×0
  • sqrt(−1)
  • 一般来说“无效操作”(我不确定是否不超过上面三个)

看看你的代码:无穷大乘以 0 是可能的,是吗?

编辑:

  • 0 <= s <= +inf

  • 1 <= m <= +inf

s / m:

  • +inf / +inf 确实会产生负 NaN (我测试过)

我认为这是唯一产生 NaN 的东西。

Taken from wikipedia -> special values -> nan

  • 0/0
  • ∞×0
  • sqrt(−1)
  • in general "invalid operations" (I am not sure wether there are not more than the three above)

Looking at you code: infinity times 0 is possible, is it?

edit:

  • 0 <= s <= +inf

  • 1 <= m <= +inf

s / m:

  • +inf / +inf does indeed make minus NaN (I tested it)

I think that's the only thing that makes a NaN.

握住我的手 2024-11-02 18:36:43

如果您可以将 x 保持在 0 和 FLT_MAX 之间(在我的例子中为 3.40E+38),您的 gp 函数将不会返回 NaN。

在此处输入图像描述

If you can keep x between 0 and FLT_MAX (3.40E+38 in my case), your gp function will not return NaN.

enter image description here

风柔一江水 2024-11-02 18:36:43

您在评论中说您只使用 *+-

[编辑:您后来说过您还使用了 pow 和除法,这引入了一些获得 NaN 的额外方法。例如,如果参数 x 是一个很大的负值,那么 pow(e,-x) 就是无穷大,因此您可以轻松地最终计算无穷大/无穷大,这是另一个NaN]

因此,如果您有 IEEE 浮点,则假设此摘要是正确的,生成 NaN 的唯一方法是:

  1. 通过超出范围生成正无穷大或负无穷大,
  2. 将其乘以零。

或:

  1. 生成一个正无穷大和一个负无穷大,
  2. 将它们相加(或等效地,减去两个相同符号的无穷大)。

因此,如果您检查并捕获无穷大,您也不必担心 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 parameter x is a large negative value then pow(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:

  1. Generate a positive or negative infinity by going out of range,
  2. Multiply it by zero.

or:

  1. Generate a positive and a negative infinity,
  2. Add them (or equivalently, subtract two infinities of the same sign).

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 ;-)

梦毁影碎の 2024-11-02 18:36:43

编辑尝试使用double而不是float

可能取决于您使用的编译器,但一般选项是:

  • 变量太小
  • 变量太大
  • 除以 0(零)

EDIT Try use double instead of float.

Probably it depends to compiler you using but general option is:

  • variable is too small
  • variable is too big
  • divide by 0 (zero)
心在旅行 2024-11-02 18:36:43

这正是启用和捕获浮点异常的用例。这样您就可以准确检测 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文