IEEE 浮点表示
我创建了以下程序来查找浮点数的位模式。但我得到了不同然后我计算:
#include<stdio.h>
int main(void){
float f = 1.234;
char *ch;
ch = (char *)(&f);
printf("\n%d\n", *ch);
ch++;
printf("\n%d\n", *ch);
ch++;
printf("\n%d\n", *ch);
ch++;
printf("\n%d\n", *ch);
// printf("%d %d %d %d", *ch, *(ch+1), *(ch+2), *(ch+3));
printf("\n%f %e", f, f);
return 0;
}
它给了我输出:
-74
-13
-99
63
1.234000 1.234000e+00
这意味着什么,因为我期望位模式为:
00111111 10111011 11100111 0110110
我错了,请纠正我
I have created following program to find the bit pattern of floating point no. but i got different then i calculated:
#include<stdio.h>
int main(void){
float f = 1.234;
char *ch;
ch = (char *)(&f);
printf("\n%d\n", *ch);
ch++;
printf("\n%d\n", *ch);
ch++;
printf("\n%d\n", *ch);
ch++;
printf("\n%d\n", *ch);
// printf("%d %d %d %d", *ch, *(ch+1), *(ch+2), *(ch+3));
printf("\n%f %e", f, f);
return 0;
}
It gives me output:
-74
-13
-99
63
1.234000 1.234000e+00
What does it mean because i was expecting bit pattern as:
00111111 10111011 11100111 0110110
where i am wrong please correct me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定你从哪里得到这个位模式。
对于 IEEE-754,
1.234
相当于0x3F9DF3B6
的底层表示(参见例如 http://babbage.cs.qc.edu/IEEE-754/Decimal.html)。所以我们有:根据您的系统字节顺序,您可能会发现这些字节以其他顺序出现。
I'm not sure where you got that bit pattern from.
For IEEE-754,
1.234
is equivalent to an underlying representation of0x3F9DF3B6
(see e.g. http://babbage.cs.qc.edu/IEEE-754/Decimal.html). So we have:Depending on your system endianness, you might find that these bytes come up in the other order.
标准 C 中没有内置二进制
printf
格式。如果这是您想要的输出格式,您需要编写自己的格式。您可以通过使用%x
查看十六进制输出来更接近;也许这会给你带来你需要的东西?There is no binary
printf
format built into standard C. You'll need to write your own if that's the output format you want. You could get closer by using%x
to see hexadecimal output; maybe that will get you what you need?您期望的位模式是错误的,它应该是:
它产生的数字与您得到的数字完全相同。
符号、指数和有效值为:
0, 127, 1962934
The bit pattern that you expect is wrong, it should be:
which produces exactly the numbers you got.
The sign, exponent and significant are:
0, 127, 1962934