I am using a library which has fixed point variables.The library is called libfixmath.
I am trying to see how a number gets converted from a fix16_t to a float type. I understand it uses 32 bits (16 MSB bits for integers and 16 LSB bits for decimals) to represent a number.
This is the code I tried, here "sum" is of type fix16_t.
float in_sum = fix16_to_float(sum);
printf("fix16 type sum:%u\n",sum);
printf("float type sum:%f\n",in_sum);
I display the "sum" in an unsigned integer format, again I'm not sure the best format to display the "sum" value.
A sample output I got is:
fix16 type sum:4064807395
float type sum:-3511.961426
I had a look at the conversion function:
static inline float fix16_to_float(fix16_t a) { return (float)a / fix16_one; }
Where fix16_one
is 65536.
To get a better understanding I want to be able to convert this manually, but I do not know how. I am confused with the type casting (float)a
.
Another question I have is, are the 16 integer bits signed or unsigned?
If fix16_t
is an integer type, then a / 65536
would be an integer division, without any decimals. For example 65535 / 65536
is not the floating point value 0.99998
, but the integer value 0
.
The casting of a
converts a
and therefore the whole expression into a floating point division, with decimals.
Using the values:
-230159901 / 65536
will result in the integer value -3511
.(float)-230159901 / 65536
will result in the float
value -3511.961426
.