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.
总结以上是内存溢出为你收集整理的c – 检查浮子是否不是数字全部内容,希望文章能够帮你解决c – 检查浮子是否不是数字所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)