|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
perfect !
thanks ![]() |
|
#4
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Weird floating point question |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|