IEEE 浮点表示

发布于 2024-11-08 14:51:00 字数 594 浏览 0 评论 0原文

我创建了以下程序来查找浮点数的位模式。但我得到了不同然后我计算:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

-柠檬树下少年和吉他 2024-11-15 14:51:00

我不确定你从哪里得到这个位模式。

对于 IEEE-754,1.234 相当于 0x3F9DF3B6 的底层表示(参见例如 http://babbage.cs.qc.edu/IEEE-754/Decimal.html)。所以我们有:

0x3F = 00111111 =  63
0x9D = 10011101 = -99 (as a signed char)
0xF3 = 11110011 = -13
0xB6 = 10110110 = -74

根据您的系统字节顺序,您可能会发现这些字节以其他顺序出现。

I'm not sure where you got that bit pattern from.

For IEEE-754, 1.234 is equivalent to an underlying representation of 0x3F9DF3B6 (see e.g. http://babbage.cs.qc.edu/IEEE-754/Decimal.html). So we have:

0x3F = 00111111 =  63
0x9D = 10011101 = -99 (as a signed char)
0xF3 = 11110011 = -13
0xB6 = 10110110 = -74

Depending on your system endianness, you might find that these bytes come up in the other order.

热情消退 2024-11-15 14:51:00

标准 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?

遇见了你 2024-11-15 14:51:00

您期望的位模式是错误的,它应该是:

{"00111111", "10011101", "11110011", "10110110"} = {63, -99, -13, -74}

它产生的数字与您得到的数字完全相同。

符号、指数和有效值为:
0, 127, 1962934

The bit pattern that you expect is wrong, it should be:

{"00111111", "10011101", "11110011", "10110110"} = {63, -99, -13, -74}

which produces exactly the numbers you got.

The sign, exponent and significant are:
0, 127, 1962934

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文