测试 Float 值是否为 NaN

发布于 2024-10-13 18:38:29 字数 870 浏览 8 评论 0原文

可能的重复:
检查双精度数(或浮点数)是否为 C++ 中的 nan

我需要检查 float 是否为 NaN。通过浏览一些链接,我找到了最常见的检查。

FLOAT32 f32_test_NaN = (some_value);
if (f32_test_NaN == f32_test_NaN)
{
    //do something;
}
else
{
    // do something;
}

但这似乎对我不起作用。我的代码如下:

FLOAT32 test_NaN = 0x414570A3;//some value - is this ok?

在GDB上调试:

(gdb) p test_NaN
$1 = 1.09506982e+09

(gdb) p/x test_NaN
$2 = 0x41457080 // Hex is not same as init value - What is compiler doing?

所以在我的例子中,test_NaN等于test_NaN

如果需要进行任何编译器设置,请告诉我。我在Solaris 上运行。或者有没有其他方法可以检查相同的情况。

提前致谢。

Possible Duplicate:
Checking if a double (or float) is nan in C++

I have a requirement to check if float is NaN. By going through some of the links I found the most common check.

FLOAT32 f32_test_NaN = (some_value);
if (f32_test_NaN == f32_test_NaN)
{
    //do something;
}
else
{
    // do something;
}

But this does not seem to work for me. My code is as follows:

FLOAT32 test_NaN = 0x414570A3;//some value - is this ok?

Debugging on GDB:

(gdb) p test_NaN
$1 = 1.09506982e+09

(gdb) p/x test_NaN
$2 = 0x41457080 // Hex is not same as init value - What is compiler doing?

So in my case test_NaN is equal to test_NaN.

Please let me know if any compiler setting has to be done. I am running on solaris. Or is there any other way to check the same.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

楠木可依 2024-10-20 18:38:29

包含 math.h 并使用 int isnan(x)。不要忘记使用 -lm 链接

Include math.h and use int isnan(x). Don't forget to link with -lm

羅雙樹 2024-10-20 18:38:29

如果 不可用,则执行以下操作:

if (x != x)
{
    // x is NaN
}

If <math.h> is not available, then do this:

if (x != x)
{
    // x is NaN
}
贱贱哒 2024-10-20 18:38:29

if (x != x)

对于 x = 0x7FBFFFFF(符号位 0,a = 0,其余位 1)

http://en.wikipedia.org/wiki/NaN

IEEE 浮点标准单精度(32 位)NaN 的逐位示例:s111 1111 1axx xxxx xxxx xxxx xxxx xxxx,其中 s 是符号,x 是有效负载,a 确定 NaN 的类型。如果 a = 1,则它是一个安静的 NaN;如果 a 为零且有效负载非零,则它是一个信令 NaN

if (x != x)

For x = 0x7FBFFFFF (sign bit 0, a = 0, rest of bits 1)

http://en.wikipedia.org/wiki/NaN

A bit-wise example of a IEEE floating-point standard single precision (32-bit) NaN: s111 1111 1axx xxxx xxxx xxxx xxxx xxxx where s is the sign, x is the payload, and a determines the type of NaN. If a = 1, it is a quiet NaN; if a is zero and the payload is nonzero, then it is a signaling NaN

等风来 2024-10-20 18:38:29

问题可能出在您的初始化中(至少解释了您在 gdb 中看到的值):

FLOAT32 test_NaN = 0x414570A3;

给定的十六进制值被视为整数并转换为浮点数(带有指数和值),这意味着它以不同的格式存储。

如果你想强制浮点数内的位,那么你需要 memcpy:

FLOAT32 test_NaN;
memcpy(&test_NaN, 0x414570A3, 4);

The problem is maybe in your initialization (at least that explains the value you see in gdb) :

FLOAT32 test_NaN = 0x414570A3;

The hex value given is considered as an integer and converted to float (with exponent and value) meaning it is stored in a different format.

If you want to force the bits inside the float, then you need to memcpy:

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