Solved

# binary to float

Posted on 2003-11-06

I have written a simple program which gets a float binary ( like 110.01 ) and returns

however it does not work where have done wrong?

if I enter 11.10 the porgram give nothing

if I enter 11.01 the program gives 3.00

why does the program stops when it sees a 1 after ' . '

moreover

enterin 100 gives 5 instead of 4

I would like to put a if (binNum%2=0{

sum=sum-1}

does this influence the value of binNum?

#include <stdio.h>

int bin2dec(int binNum) {

int x;

int sum;

sum=0;

x=1;

do {

sum=sum+x;

x*=2;

} while((binNum/=10)>0);

return sum;

}

float bintofloat(float binNum2){

float y;

float summ;

summ=0.0;

y=0.5;

while (binNum2*10.0>1.0){

summ=summ+y;

y*=0.5;

}

return summ;

}

int main()

{

int a;

int sum;

float summ;

float binNum;

float binNum2;

float answer;

scanf("%f",&binNum);

a=(int)binNum;

binNum2=binNum-a;

sum=bin2dec(a);

summ=bintofloat(binNum2);

answer=sum+summ;

printf("%f",answer);

return 0;

}