C IEEE-754浮点数二进制表示

C IEEE-754浮点数二进制表示,c,binary,ieee-754,C,Binary,Ieee 754,我正在解决一个关于在线法官的问题: 我需要用二进制表示IEEE-754浮点数 这是我的密码: #include <stdio.h> void binaryprint(); int main(){ float x; while(scanf("%f",&x) != EOF){ //File end with EOF unsigned int y = *(unsigned int*)&x; i

我正在解决一个关于在线法官的问题:

我需要用二进制表示IEEE-754浮点数

这是我的密码:

#include <stdio.h>

void binaryprint();

int main(){
    float x;
    while(scanf("%f",&x) != EOF){                //File end with EOF
        unsigned int y = *(unsigned int*)&x;
        if(x == 0){                                  // INput = 0 -> 0*32
            for(int i = 0 ; i < 32 ; i++){
                printf("%d",0);
            }
        }
        else {
            if(x > 0) printf("%d",0);     //0 means positive but I find c will not print it out
            binaryprint(y);              //transfer to binary represent
        }
        printf("\n");
    }
}

void binaryprint(unsigned int x){
    if(x != 1){
        binaryprint(x/2);
        printf("%d",x%2);
    }
    else{
        printf("%d",1);
    }
}

#包括
void binaryprint();
int main(){
浮动x;
而(scanf(“%f”,&x)!=EOF){//文件以EOF结尾
无符号整数y=*(无符号整数*)&x;
如果(x==0){//INput=0->0*32
对于(int i=0;i<32;i++){
printf(“%d”,0);
}
}
否则{
如果(x>0)printf(“%d”,0);//0表示正数,但我发现c不会打印出来
binaryprint(y);//传输到二进制表示
}
printf(“\n”);
}
}
无效二进制打印(无符号整数x){
如果(x!=1){
二进制打印(x/2);
printf(“%d”,x%2);
}
否则{
printf(“%d”,1);
}
}
但是我得到了一些错误的答案,因为我不知道测试用例, 我不知道是否有例外导致了错误的答案。
谢谢你的帮助

下面是一个some程序,注释中有解释,用于处理所示的案例。(它不处理无穷大或N。)

#包括
#包括
内部主(空)
{
浮动x;
而(scanf(“%f”,&x)==1)
{
//打印符号位。
putchar('0'+(x<0));
if(x<0)
x=-x;
/*将x乘以或除以2得到间隔中的有效位
[1,2]。同时,计算操作数,以确定需要什么
需要二的幂(指数)才能将有效位缩放到
x的原始值。
*/
int指数=0;
而(x<1)
{
指数-=1;
//如果我们达到低于正常范围,停下来。
如果(指数<-126)
打破
x*=2;
}
而(2 i&1),;
/*跳过有效位的前导位并打印尾随位
位。
*/
x-=(int)x;
对于(int i=0;i<23;++i)
{
x*=2;
int位=x;
x-=位;
putchar('0'+位);
}
putchar('\n');
}
}

根据问题中给出的限制,编写您自己的测试数据。注意一些特殊情况。浮点数的范围是-10^20~10^20,我已经尝试过了,它与我的程序相同。当输入数低于正常值时,您的代码会失败。您需要一种更简单的方法来转换为二进制输出,非递归(这是一种“school exercise”方法),精确到32位。二进制输出在其他情况下也有错误,但对于给定的示例数据它“碰巧工作”。此外,您还有一个不符合要求的函数原型
void binaryprint();
!我发现当我的输入介于0到2之间时,它只会打印31位,我需要打印另一个“0”,但我很好奇为什么
#include <math.h>
#include <stdio.h>

int main(void)
{
    float x;
    while (scanf("%f", &x) == 1)
    {
        //  Print the sign bit.
        putchar('0' + (x < 0));
        if (x < 0)
            x = -x;

        /*  Multiply or divide x by two to get a significand in the interval
            [1, 2).  At the same time, count the operations to figure what
            power of two (in Exponent) is needed to scale that significand to
            the original value of x.
        */
        int Exponent = 0;
        while (x < 1)
        {
            Exponent -= 1;
            //  If we reach the subnormal range, stop.
            if (Exponent < -126)
                break;
            x *= 2;
        }
        while (2 <= x)
        {
            Exponent += 1;
            x /= 2;
        }

        /*  Encode the exponent by adding the bias of the IEEE-754 binary32
            format.
        */
        Exponent += 127;

        //  Print the seven bits of the exponent encoding.
        for (int i = 7; 0 <= i; --i)
            putchar('0' + (Exponent >> i & 1));

        /*  Skip the leading bit of the significand and print the 23 trailing
            bits.
        */
        x -= (int) x;
        for (int i = 0; i < 23; ++i)
        {
            x *= 2;
            int bit = x;
            x -= bit;
            putchar('0' + bit);
        }

        putchar('\n');
    }
}