IEEE REPRESENTATION OF FLOATING POINT NUMBER
An IEEE-754 float (4 bytes) or double (8 bytes) has three components (there is also an analogous 96-bit extended-precision format under IEEE-854):
-> a sign bit telling whether the number is positive or negative,
->an exponent giving its order of magnitude,
->an mantissa specifying the actual digits of the number.
Using single-precision floats(32 bit) as an example, here is the bit layout:-
we have to represent it in form of ->1.M *2^(+/-E)
where:-
->M is the mantissa which is to be represented in 23 bit for single precision number.
->E is the exponent which is to be represented in 8 bit for single precision number.
eg 1:
now suppose the floating point no: is 11.27
->signbit in 1 bit is: 1
->mantissa in 23 bit is: :0 1 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 1 0 1 0 1 1
->exponent is 8 bit: 10000010
eg 2:
now suppose the floating point no: is 157.38
->signbit in 1 bit is: 1
->mantissa in 23 bit is: :0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 0 1 0 0 0 1 1 1
->exponent is 8 bit: 10000110
->signbit in 1 bit is: 1
->mantissa in 23 bit is: :0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 0 1 0 0 0 1 1 1
C CODE FOR IEEE REPRESENTATION OF FLOATING POINT NUMBER
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
static int r[23],mant[24];
int dec2bin(int n,int *arr)
{
int i,p;
p=n;
for(i=0;i<23;i++)
arr[i]=0;
i=22;
do
{
arr[i]=n%2;
n=n/2;
i--;
}
while(n!=0);
if(p<0)
{
printf("****negative number****\n");
printf("do enter a positive number\n");
}
else
{
printf("the binary equivalent of %d is:",p);
for(i=0;i<23;i++)
printf("%d",arr[i]);
}
printf("\n");
}
int dec2bin4exp(int n,int *arr)
{
int i,p;
p=n;
for(i=0;i<8;i++)
arr[i]=0;
i=7;
do
{
arr[i]=n%2;
n=n/2;
i--;
}
while(n!=0);
printf("the binary equivalent of %d is ",p);
printf("(i.e the exponent part in 8 bit is:)");
for(i=0;i<8;i++)
printf("%d",arr[i]);
printf("\n");
}
int sign(float n)
{
int sign;
if(n>0)
sign=0;
else
sign=1;
printf("the signbit=%d\n",sign);
}
int rep(int *arr)
{
int i,j,z=0,t=0,l,flag=0,power,sign,bias,k=0;
for(i=0;i<23;i++)
{
if(arr[i]==1)
z=i;
if(z>0)
{
for(l=z+1;l<23;l++)
mant[k++]=arr[l];
break;
}
}
bias=k+127;
//printf("K=%d",k);
for(j=0;j<23;j++)
{
mant[k++]=r[j];
}
printf("the mantissa part in 23 bit is:");
for(i=0;i<23;i++)
{
printf("%d ",mant[i]);
}
printf("\n");
printf("------------------------------------------------------\n");
printf(" bias value is=%d\n",bias);
dec2bin4exp(bias,r);
}
void pt2bin(double d)
{
int i;
static double ar[23],f=0.0;
ar[0]=d;
// printf("\nar=%f\n",ar[0]);
for(i=0;i<23;i++)
{
f=ar[i] *2;
r[i]=(int)f;
ar[i+1]=f-r[i];
}
printf("The binary equivalent of %lf is:",d);
int ii;
for(ii=0;ii<23;ii++)
printf("%d ",r[ii]);
printf("\n");
}
int main()
{
double f,deci;
int real,arr[23],arr1[5];
printf("enter a floating point number\n");
scanf("%lf",&f);
real=f;
deci=f-real;
printf("------------------------------------------------------\n");
printf("the real part of the number is:=%d\n",real);
printf("the decimal part of the number is:=%lf\n",deci);
printf("------------------------------------------------------\n");
dec2bin(real,arr);
pt2bin(deci);
printf("------------------------------------------------------\n");
sign(f);
rep(arr);
printf("------------------------------------------------------\n");
return 0;
}
No comments:
Post a Comment