Sunday, April 8, 2012

IEEE 754 representation of any floating point number

#include<stdio.h>
int power(int,int);
void main()
{
float a,f,r,x,temp,sum,g;
int
n,i,j,u,o,b[30],c[30],B[30],Z[30],e[10],m[25],F[40],t,k,y,z,count,exp,h,d;
clrscr();
printf("enter the number
");
scanf("%f",&a);
g=a;
if(a<0)
{
a=0-a;
n=a;
}
else
n=a;
f=a-n;

/* conversion of integer part to binary */

i=0;
while(n!=0)
{
b[i]=n%2;
n=n/2;
i++;
}
/* arranging the bits of the binary equalent of integer part*/

for(j=i-1,k=0;j>=0;j--,k++)
c[k]=b[j];
if(k==0)
{
c[k]=0;
k=k+1;
}
/* conversion of floating part to binary */
sum=0;
for(i=1;i<=11;i++)
{
    t=power(2,i);
    r=t;
    x=1/r;
    sum=sum+x;
    temp=sum;
    if(temp <= f)
        {
        B[i-1]=1;
        sum=temp;
        }
        else
        {
        B[i-1]=0;
        sum=temp-x;
        }
}
/* arranging the bits of the binary equalent of floating part*/
for(i=0,z=k;i<=10;i++,z++)
c[z]=B[i];

/* Combined representation */
//printf("
the equavalent binary representation of the floating point
number is:
");
//for(y=0;y<=z-1;y++)
//  {
//    printf("%d",c[y]);
//    if(y==k-1)
//    printf(".");
//  }



/* IEEE 754 format representation */
count=k-1;
printf("The IEEE 754 standard floating point number allignment is
");
exp=127+count;
i=0;
while(exp!=0)
{
Z[i]=exp%2;
exp=exp/2;
i++;
}
if(count==0)
{
Z[i]=0;
i++;
}
if(g<0)
printf(" The sign value is %d",1);
else
printf(" The sign value is %d",0);
/* Arranging the bits in order for exponent*/
for(j=i-1,k=0;j>=0;j--,k++)
e[k]=Z[j];



printf("
 The value of exponent is : ");
for(u=0;u<=k-1;u++)
printf("%d",e[u]);


/* Arranging the bits in order for Mantissa*/

printf("
 The value of mantissa is :");
d=z-1;
for(y=1,o=0;o<=22;y++,o++)
{
  if(o<d)
  m[o]=c[y];
  else
  m[o]=0;
 printf("%d",m[o]);
}

printf("
The FINAL REPRESENTATION IS : ");



for(h=0;h<=31;h++)
{

    if(h==0)
    {
     if(g<0)
     F[0]=1;
     else
     F[0]=0;
    }
    if(h<=k && h!=0)
    F[h]=e[h-1];
    else if(h>k && h!=0)
    F[h]=m[h-9];

printf("%d",F[h]);
}
getch();
}



int power(int w, int d)
{
int pro,i;
pro=1;
for(i=1;i<=d;i++)
pro=pro*w;
return(pro);
}

No comments:

Post a Comment