重新用C浮动presentation [英] Representation of float in C
问题描述
我试图用这个code,了解在C浮点再presentation(均浮动
和 INT
4个字节我的机器上):
I was trying to understand the floating point representation in C using this code (both float
and int
are 4 bytes on my machine):
int x = 3;
float y = *(float*) &x;
printf("%d %e \n", x, y);
我们知道X的二进制重新presentation将以下
We know that the binary representation of x will be the following
00000000000000000000000000000011
所以我本来期望y将被重新psented如下$ P $
Therefore I would have expected y to be represented as follows
-
符号位=
0
指数(位2-9左起)= 0
Exponent (bits 2-9 from left) = 0
尾数(位10-32): 1 + 2 ^( - 22)+2 ^( - 23)
Mantissa (bits 10-32): 1 + 2^(-22)+2^(-23)
通往 Y =(-1)^ 0 * 2 ^(0-127)*(1 + 2 ^( - 22)+ 2 ^( - 23))= 5.87747E-39
不过我的程序打印出
3 4.203895e-45
也就是说,Y的值的4.203895e-45
而不是 5.87747E-39
如我所料。为什么会发生这种情况。我在做什么错了?
That is, y has the value 4.203895e-45
instead of 5.87747E-39
as I expected. Why does this happen. What am I doing wrong?
P.S。我也直接从GDB打印的值,因此它不与printf命令有问题。
P.S. I have also printed the values directly from gdb so it is not a problem with the printf command.
推荐答案
与所有的0指数领域的IEEE浮点数被规格化。这意味着,在尾数前面的隐式1不再是活动的。这使得真正的小号码重新presented。请参见本作更多的解释维基百科的文章。在你的榜样,其结果将是3 * 2 ^ -149
IEEE floating point numbers with exponent fields of all 0 are 'denormalized'. This means that the implicit 1 in front of the mantissa no longer is active. This allows really small numbers to be represented. See This wikipedia article for more explanation. In your example the result would be 3 * 2^-149
这篇关于重新用C浮动presentation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!