This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
                            
                        
                    
                
                                7年前关闭。
            
                    
我必须用浮点数表示一个二进制数。
我有一个十六进制数FFFF,当我将该十六进制数转换为Binary时,我得到的对应二进制数为1111111111111111。
我的英特尔处理器使用的存储格式为32位,即符号1位,指数8位和尾数23位。
我有一些主意但是很困惑。
有人可以帮我解决这个二进制数对应的浮点值吗?

最佳答案

32个

只需尝试一下:

#include <stdio.h>

int main() {
  union {
    unsigned i;
    float f;
  } u;
  u.i = 0xffffffff;
  printf("%f\n", u.f);
  return 0;
}


打印-nan。此实验假设您确实要0xffffffff而不是0xffff作为问题陈述。

查看http://en.wikipedia.org/wiki/Single_precision,您会发现指数0xFF与非零有效数一起被视为NaN。

16个

如果您确实只在0xFFFF之后,如您的问题所写,那么代码将显示0.000000,但是将%f更改为%g会得到9.18341e-41。这是因为整数和浮点数都使用相同的字节序,即,您所讨论的是与位模式0x0000ffff对应的浮点数。

在那里您会看到符号现在为零(即正数),指数为零和有效位数为非零。根据同一维基百科的文章,这表示subnormal number。因此,这实际上是0xffff∙2-149 = 65535∙2-149。

关于c - 如果我的二进制数为1111111111111111并且Intel处理器使用的存储格式为32位,则float的值是多少? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13673045/

10-13 07:08