将7FFFFFFF转换为十进制等值?

时间:2012-10-01 19:00:27

标签: hex nan

我在某个地方看到这是一个特例,+ NaN从0x7F800001变为0x7FFFFFFF。答案是+ NaN?

2 个答案:

答案 0 :(得分:2)

如果将7FFFFFFF解释为IEEE754 32位浮点数,则是,7FFFFFFF为NaN。您可以通过查看Single-precision floating-point format的维基百科页面来了解这些内容。我写了这个小C程序来说明这一点:

#include <stdio.h>

int main(){

  unsigned u0 = 0x7FFFFFFF;
  unsigned u1 = 0x7F800001;
  unsigned u2 = 0x7F800000;
  unsigned u3 = 0x7F7FFFFF;

  // *(float*)&u0 causes the data stored in u0 to be interpreted as a float
  printf("%e\n", *(float*)&u0);  // This gives nan
  printf("%e\n", *(float*)&u1);  // This also gives nan
  printf("%e\n", *(float*)&u2);  // This gives inf
  printf("%e\n", *(float*)&u3);  // This gives 3.402823e+38, the largest possible IEEE754 32-bit float

  // The above code only works because sizeof(unsigned)==sizeof(float)
  printf("%u\t%u\n", sizeof(unsigned), sizeof(float));

  // Remember that nan is only for floats, u0 is a perfectly valid unsigned.
  printf("%u\n", u0);            // This gives 2147483647

}

同样,必须提到NaN仅作为浮点数存在。

答案 1 :(得分:0)

+ NaN是浮点数的一个特殊值(它没有十进制等值。它是“非数字”)。 如果你只想要整数的十进制表示,它有7FFFFFFF作为十六进制表示,则不涉及浮点,并且没有+ NaN