Monday, 15 April 2013

CONVERSION OF FLOATING-POINT NUMBERS TO THEIR BINARY EQUIVALENT AND VICE-VERSA


BINARY TO DECIMAL CONVERSION

For the conversion of the floating point binary numbers into their decimal equivalent we use the following rules:
  1. The integral part of the floating point number is converted into the decimal equivalent using the usual positional weight notation. The following figure clearly explains this thing:-
  2. The conversion of the fractional part of the number into its decimal equivalent can be summarized in the following table.

BinaryDecimal FractionDecimal Value
.11/2.5
.011/4.25
.0011/8.125
.00011/16.0625
.000011/32.03125

DECIMAL TO BINARY CONVERSION

Simple conversion of simple integral part of any decimal can be portrayed as follows:- 

The following figure explains the rule for conversion of fractional part of decimal number to binary equivalent:-


I hope, by reading the above article, you are clear with both types of conversions at their worst scenario, that is when the number to be converted includes fractional part.
Now, after giving a detailed description of conversion, I would like the user to have a look at my source code in C that performs all the above specified conversions. My source code contains two functions that easily perform the following two conversions:-
  • Binary to decimal
  • Decimal to binary 

The user can give the input as per his/her wish including the floating point numbers.

SOURCE CODE

/********************************************************************************/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 30

int fraction[max],integer[max];

void d2b();
void b2d();

int main()
{
int i,cnum,ch;
float num,f;
clrscr();
while(1)
{
printf("YOU HAVE FOLLOWING CHOICES : \n");
printf("1.CONVERT DECIMAL TO BINARY\n");
printf("2.CONVERT BINARY TO DECIMAL\n");
printf("3.EXIT\n\n");
printf("ENTER YOUR CHOICE : ");
scanf("%d",&ch);
printf("\n");
switch(ch)
{
case 1:
d2b();
break;
case 2:
b2d();
break;
case 3:
exit(1);
default:
printf("YOU HAVE ENTERED A WRONG CHOICE\n");
}
printf("\nPRESS ANY KEY TO CONTINUE...\n");
getch();
clrscr();
}

getch();
return 1;
}


void d2b()
{
float num,f;
int i,cnum,tag;
printf("ENTER ANY DECIMAL NUMBER : ");
scanf("%f",&num);
printf("\n");
f=num-(int)num;
for(i=0;i<max;i++)
{
f=f-(int)f;
fraction[i]=(int)(f*2);
f*=2;
}

cnum=(int)num;
for(i=max-1;i>=0;i--)
{
if(cnum%2==0)
{
integer[i]=0;
}
else
integer[i]=1;
cnum/=2;
}

printf("BINARY EQUIVALENT = ");
tag=0;

for(i=0;i<max;i++)
{
if(integer[i]||tag)
{
tag=1;
printf("%d",integer[i]);
}
}
printf(".");

for(i=0;i<7;i++)
printf("%d",fraction[i]);

printf("\n");

}


void b2d()
{
double num,fnum,result=0;
long inum,i=0,a=0;
printf("ENTER ANY BINARY NUMBER : ");
scanf("%lf",&num);
printf("\n");

inum=num;
fnum=num-(long)num;
printf("%ld\t%lf\n",inum,fnum);

while(inum)
{
result+=(inum%10)*pow(2,i++);
inum/=10;
}

for(i=0;i<max;i++)
{
a--;
fnum*=10;
if(fnum>=1)
{
fnum-=1;
result+=pow(2,a);
}
}
printf("DECIMAL EQUIVALENT = %lf\n",result);
}

/******************************************************************************/

OUTPUT




No comments:

Post a Comment