#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    119
    Rep Power
    12

    Weird floating point question


    hello,

    Ive run into a problem that I think there may be a good solution for.

    I have a single precision floating point value given to me in the following format:

    3 bytes
    -------------------------------------
    SFFFFFFF FFFFFFFF EEEEEEEE

    thats 1 Sign bit, 15 Mantissa, 8 exponent.
    (a different order than the IEEE standard)

    Right now I just have the 3 bytes as unsigned char variables to hold these values. Now the question is, Is there an easy way to turn these 3 seperate bytes back into a floating point number?

    I can manually shift them around but that seems way too hard.

    Thanks in advance : )
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    I think you're pretty much stuck with shifting them around, but that shouldn't be too much work. Only the exponent will need to be shifted.

    Let's see, off the top of my head:
    Code:
    //3 bytes
    //-------------------------------------
    //SFFFFFFF FFFFFFFF EEEEEEEE
    
    typedef unsigned char BYTE;
    
    float ConvertFlt(BYTE flt_in[])
    {
        BYTE flt_out[4];
        BYTE temp;
    
        flt_out[3] = 0;
        flt_out[2] = flt_in[1];
        flt_out[1] = flt_in[0] & 0x7F;  // mask out sign bit
        if (flt_in[2] & 0x01)  // "shift" exp LSB in from the left
            flt_out[1] |= 0x80;
        flt_out[0] = (flt_in[0] & 0x80) | ((flt_in[2] >> 1) & 0x7F);
        return *((float*)&flt_out[0]);
    }
    Bear in mind that this is ignoring Intel's reversed-order of multibyte data ("little-endian"). In that case, you would have to have indexed the flt_out bytes in the reversed order. I figured that if I did that here, then it would just make the code confusing.

    I am also ignoring any deviation of the exponent's format or of the mantissa's from the IEEE standard.

    I also have not tested this code, but I think it should be close.

    Finally, float has one more byte than your simple floating-point type, but adding the fourth byte of zero should not change the precision of the original.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    119
    Rep Power
    12
    perfect !

    thanks :)
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    You could also possibly use the union structure to do your conversions. Declare a union with two members, a float and a char array. Now, all you need to do is assign to one of the members and then, read from the other one, for the conversion. The following code converts between float and chars:
    Code:
    #include <stdio.h>
    typedef union {
      char c[sizeof(float)];
      float f;
    } Foo;
    
    int main (void) {
      Foo bar;
      int x;
      bar.f = 12.34;
      printf("Float %f is converted to ", bar.f);
      for (x = 0; x < sizeof(float); x++)
        printf("%d ", (int)bar.c[x]);
      printf("\n");
    
      bar.c[0] = -92;
      bar.c[1] = 112;
      bar.c[2] = 69;
      bar.c[3] = 65;
      printf("Chars ");
      for (x = 0; x < sizeof(float); x++)
        printf("%d ", (int)bar.c[x]);
      printf("is converted to %f\n", bar.f);
    
      return 0;
    }
    Hope this helps.

IMN logo majestic logo threadwatch logo seochat tools logo