Facebook Badge

Thursday, September 2, 2010

Java Program for IEEE754 representation of floating point number




/*This program can find IEEE754 representation of a floating point number upto the range of long
   i.e. 9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

The program gives error result for inputs like 9,223,372,036,854,775,809.23  etc */


/**
 * @(#)IEEE754_Exp.java
 *
 *
 * @author Rohit Iyer
 * @version 1.00 2010/9/2
 */

 import java.util.*;

class IEEE754
{
    float n,f;
    long x;
    int sign;
    int pos;
    int[] exp=new int[8];
    int[] temp1=new int[128];   
     // since decimal point can shift up to max 127 places to the left

    int[] temp2=new int[128];   
     // since decimal point can shift up to max 127 places to the right.

    int[] mantissa=new int[23];
    int ans[]=new int [256];
   // ans[] is used for intermediate clalculations like finding the first 1 and for extra precision of mantissa
 
   
    public void getdata()
    {
        Scanner src=new Scanner(System.in);
        System.out.print("Enter a floating point number : ");
        n=src.nextFloat();
        if(n<0)
        {
        sign=1;
        n=n*-1;
        }
         else sign=0;

        x=(long)n;
        f=n-x;

    }

    public void binary()
    {
       
        int i=127;
                   do
                {
                         temp1[i]=(int)x%2;
                           x=x/2;
                          i--;
                 }
                 while(i>0);
   
        i=0;
       
       
        do
        {
        if(f!=1.0f)
        {
        temp2[i++]=(int)(f*2);
        f=(f*2)-(int)(f*2);
        if(f==1.0f)
                break;
        }
       
        } while(i<=127);

       

    }

    public void calcExp()
    {
        int i;
        for(i=0;i<128;i++)
        ans[i]=temp1[i];
       
        for(i=128;i<256;i++)
        ans[i]=temp2[i-128];

        pos=0;
        for(i=0;i<256;i++)
        if(ans[i]==1)
        {
        pos = i+1;
        break;
        }
       

        int ex=127+(127-i);   
        // calculates how many places shifting is required

        i=7;               //exponent is 8 bit
                   do
        {
             exp[i]=ex%2;
                           ex=ex/2;
                          i--;
                         }
                         while(i>=0);

    }

    public void calcMantissa()
    {
        int i,j=0;
        for(i=pos;j<23;i++)
        mantissa[j++]=ans[i];
    }

    public  void showdata()
    {
        int i;
        System.out.print("S\t\tE\t\t M\n");
        System.out.print(sign+"\t");
        for(i=0;i<8;i++)
        System.out.print(exp[i]);
        System.out.print("\t");
        for(i=0;i<23;i++)
        System.out.print(mantissa[i]);
    }


    }
    class IEEE754_Exp
    {
        public static void main(String args[])
        {
        IEEE754 x=new IEEE754();
        x.getdata();
        x.binary();
        x.calcExp();
        x.calcMantissa();
        x.showdata();
        }
    }

No comments:

Post a Comment