加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 检查浮子是否不是数字

发布时间:2020-12-16 09:57:32 所属栏目:百科 来源:网络整理
导读:此代码来自UE4,我在理解它时遇到了一些麻烦. static FORCEINLINE bool IsNaN( float A ) { return ((*(uint32*)A) 0x7FFFFFFF) 0x7F800000;} (uint32只是一个无符号的int) 第一部分是将float解释为uint32,这是简单的部分,但为什么是按位,它有0x7FFFFFFF?告
此代码来自UE4,我在理解它时遇到了一些麻烦.

static FORCEINLINE bool IsNaN( float A ) 
{
    return ((*(uint32*)&A) & 0x7FFFFFFF) > 0x7F800000;
}

(uint32只是一个无符号的int)

第一部分是将float解释为uint32,这是简单的部分,但为什么是按位,它有0x7FFFFFFF?告诉我们的最后4位是什么?为什么我们将结果与0x7F800000进行比较?

解决方法

该定义遵循IEEE 754标准中提供的NaN定义.

In IEEE 754 standard-conforming floating point storage formats,NaNs are identified by specific,pre-defined bit patterns unique to NaNs.

0x7FFFFFFF是最大值,可以用31位表示.在这种情况下,它只是忽略位符号,因为:

The sign bit does not matter.

0x7F800000是指数字段的位掩码.

For example,a bit-wise IEEE floating-point standard single precision
(32-bit) NaN would be: s111 1111 1xxx xxxx xxxx xxxx xxxx xxxx where s
is the sign (most often ignored in applications) and x is non-zero
(the value zero encodes infinities).

其中s111 1111 1xxx xxxx xxxx xxxx xxxx xxxx,其中s = 0(通过与0x7FFFFFFF并联)并且x = 0等于0x7F800000.

运算符>使用,因为如果有意义中的任何位被设置(任何x),结果值将大于0x7F800000.

为什么要设置?因为:

The state/value of the remaining bits […] are not defined by the standard except that they must not be all zero.

资料来源:NaN on Wikipedia.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读