Question

Here i add function from my code which use to generate float number from given binary number.

Code:

double binary_float(double f) /* Function to convert binary to float.*/
{
   long     integral = 0, floatInt = 0, i = 1, temp1 = 0, k = 1;
   double   floatFract = 0, fractional = 0, floatTotal = 0;

   //Separating the integral value from the floating point variable
   integral = (long)f;

   //Separating the fractional value from the variable
   fractional = f - (long)f;

   //Converting binary to decimal
   floatInt = binary_decimal(integral);

   //Loop for converting binary to Fractional value
   while( k < 10000000 && fractional != (double)0 )
   {
      k = k * 10;
      i = i * 2;
      temp1 = (long)(fractional * k);

printf("temp: %ld, r: %lf\n", temp1, (fractional * k));

      floatFract = floatFract + (double)temp1/(double)i;

printf("fact: %lf, r: %lf\n", floatFract, ((double)temp1/(double)i));

      fractional = fractional - (double)temp1/(double)k;

printf("frac: %lf, r: %lf\n", fractional, ((double)temp1/(double)k));

   }

   //Combining both the integral and fractional binary value.
   floatTotal = floatInt + floatFract;

   return floatTotal;
}

long binary_decimal(long n)
{
   long decimal=0, i=0, rem;
   while (n!=0)
   {
      rem = n%10;
      n/=10;
      decimal += rem*pow(2,i);
      ++i;
   }
   return decimal;
}

Output:

Enter a binary number: 1010.001100
temp: 0, r: 0.011000
fact: 0.000000, r: 0.000000
frac: 0.001100, r: 0.000000
temp: 0, r: 0.110000
fact: 0.000000, r: 0.000000
frac: 0.001100, r: 0.000000
temp: 1, r: 1.100000
fact: 0.125000, r: 0.125000
frac: 0.000100, r: 0.001000

temp: 0, r: 1.000000

fact: 0.125000, r: 0.000000
frac: 0.000100, r: 0.000000
temp: 9, r: 10.000000
fact: 0.406250, r: 0.281250
frac: 0.000010, r: 0.000090
temp: 9, r: 10.000000
fact: 0.546875, r: 0.140625
frac: 0.000001, r: 0.000009
temp: 9, r: 10.000000
fact: 0.617188, r: 0.070312
frac: 0.000000, r: 0.000001
1010.001100 in binary = 10.617188 in float

In output you can see temp: 0, r: 1.000000 which says temp1 = 0 which is typecasting of 1.000000 by long.

Can any one explain me why this typecasting is not working?

Was it helpful?

Solution

OP: "temp: 0, r: 1.000000 which says temp1 = 0 which is typecasting of 1.000000 by long."
A: Conversion of double to long is working, for double is less than 1.0.

Code is printing a rounded value of the double, not its exact value, which is slightly less than 1.0. (long) some_number_slightly_less_than_1 is 0. Try printing the double with more precision "%.20le" instead of "%lf".

Given typical IEEE binary64 floating point, when code starts with a double of 1010.001100, code is really starting with a 1010.00109999999995125108.....

As to why the wrong answer of 10.617188, I suspect unposted code binary_decimal();


Code has error:

// floatFract = floatFract + (double) temp1 / (double) i;
floatFract = floatFract + (double) temp1 / (double) k;
...
// wrong result of 10.61718750000000000000
// correct result follows
10.00109989999999982047....
// or to 6 decimal places
10.001100

Minor: note that the below code only works for a double f in the range LONG_MIN to LONG_MAX.

integral = (long)f;

OTHER TIPS

temp1 is printed using %ld flag which is long int while r is printed using %lf flag for double, thus the trailing 0s.

Here what i came up with it's not fully working. There is limit of long data type so we can't enter larger number then this range in this program other then that program works gracefully.

Code:

double binary_float(double f) /* Function to convert binary to float.*/
{
   long     integral = 0, floatInt = 0;
   double   floatFract = 0, fractional = 0, floatTotal = 0;

   //Separating the integral value from the floating point variable
   integral = (long)f;
   //Separating the fractional value from the variable
   fractional = f - (long)f;

   //Converting binary to decimal
   floatInt = binary_decimal(integral);
   //Converting float value from binary to float value
   floatFract = binary_float_float(fractional);

   //Combining both the integral and fractional binary value.
   floatTotal = floatInt + floatFract;

   return floatTotal;
}

long binary_decimal(long n) /* Function to convert binary to decimal.*/
{
   long decimal=0, i=0, rem;
   while (n!=0)
   {
      rem = n%10;
      n/=10;
      decimal += rem*pow(2,i);
      ++i;
   }
   return decimal;
}

double binary_float_float(double fractional)
{
   long     temp1 = 0, k = 1, i = 1;
   double   floatFract = 0;

   //Loop for converting binary to Fractional value
   while( k < 1000000000 && fractional != (double)0 )
   {
      k = k * 10;
      i = i * 2;
      temp1 = (long)(fractional * k);
      //If value is larger then 1 it means .001100 is represented as .00109994....
      //So it's last digit and so we can break loop from that point
      if (temp1 > 1)
      {
         temp1 = 1;
         floatFract = floatFract + (double)temp1/(double)i;
         break;
      }

      floatFract = floatFract + (double)temp1/(double)i;
      fractional = fractional - (double)temp1/(double)k;
   }

   return floatFract;
}

Anyone having better idea and better way to find float value from binary number then please share it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top