Troubles with IEEE 754 floating point numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ms292606
    New Member
    • Jul 2007
    • 4

    #1

    Troubles with IEEE 754 floating point numbers

    Hi,

    I am currently working on a program to decode data that is collected on a gps receiver and i've ran into a problem. Some of the data is encoded in IEEE 754. I wrote the following functions to decode the the binary:
    Code:
    /**************************************************************************************************
    *Converts binary into IEEE 754 single percision
    ***************************************************************************************************/
    float bin2float(unsigned long binary){
          float fp;
          unsigned int s=0,e=0,f=0;
          s=binary>>31;/*sign bit*/
          e=(binary>>23&0xff);/*exponent*/
          f=binary&0x7fffff;/*fraction*/
          fp=(sign_bit(s)<<(e-127))*(1+f*(pow(2,-23)));
    
          return(fp);
    }
    /**************************************************************************************************
    *Converts binary into IEEE 754 double percision
    ***************************************************************************************************/
    double bin2double(int binary1, int binary2){//binary1=bits 0-31 binary2=bits 32-63
          double fp;
          unsigned int s=0,e=0,f=0;
          s=binary2>>31;/*sign bit*/
          e=(binary2>>20&0x7ff);/*exponent*/
          f=(binary1*pow(2,-51)+((binary2&0x7ffff)*pow(2,-21)));/*fraction*/
          fp=(sign_bit(s)*pow(2,(e-1023)))*(1+f);
    
          return(fp);
    
    }
    these return the correct values but I do not have enough processing power available to use the pow function. Any ideas on a way to decrease cycles? Is there a predefined way in C to convert the binary to IEEE 754? Any help would be appreciated I don't work with C too often. Oh almost forgot, I am running this on a linux platform. Thanks
    -Matt
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Is there some reason you can't just:

    [code=c]
    unsigned long binary = 1234;
    float f = binary;
    double d = binary;
    [/code]

    ??

    Comment

    • AdrianH
      Recognized Expert Top Contributor
      • Feb 2007
      • 1251

      #3
      Originally posted by weaknessforcats
      Is there some reason you can't just:

      [code=c]
      unsigned long binary = 1234;
      float f = binary;
      double d = binary;
      [/code]

      ??
      True. But the data is coming from somewhere else, so a cast is really what is needed.

      [code=c]
      void * address; // contains the address of the double
      double value = (double*)addres s;
      [/code]


      Adrian

      Comment

      • AdrianH
        Recognized Expert Top Contributor
        • Feb 2007
        • 1251

        #4
        Since all of your pow() calls are using a base of 2 use the >> or << operators instead. But your algorithm is probably slower then a cast.


        Adrian

        Comment

        • ms292606
          New Member
          • Jul 2007
          • 4

          #5
          thanks for the help guys, I got it working fast enough. My problem was i was using a 4 byte int. once i changed it to a long long that could store 8 bytes of data then i was able to do everything by using bit shifting.

          Comment

          • AdrianH
            Recognized Expert Top Contributor
            • Feb 2007
            • 1251

            #6
            Originally posted by ms292606
            thanks for the help guys, I got it working fast enough. My problem was i was using a 4 byte int. once i changed it to a long long that could store 8 bytes of data then i was able to do everything by using bit shifting.
            But why don't you just let the compiler/processor interpret the floating point number? Why do the manual translation?


            Adrian

            Comment

            Working...