I'm solving a question on online judge: https://acm.cs.nthu.edu.tw/problem/12237/
I need to represent IEEE-754 floating number with binary represent.
Here's my code:
#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);
}
}
But I got few wrong answer and because of I didn't know the testcase , I can't find out if there's any exception lead to the wrong answer. Thanks for your help!