Hello,
I have a strange behaviour in my project. I would like to receive a 32bit value via CAN and interprete it as a float value.
But the conversion to float seems to be wrong.
For example when I try to do the following:
int32_t fixTest = 0x3fc00000; //should be 1.5
float testValue = (float)fixTest;
In my case test Value is 1.06956e9 instead of the right value 1.5;
What am I missing here? Is something wrong with my project settings?
I am using Codewarrior 10.3 and MPC5643L (Leopard).
Thanks in advance.
Solved! Go to Solution.
You get what is specified in subject, big integer is converted to float correctly. But I see you want to treat integer as a binary representation of float. Try this:
float testValue;
*(int32_t*)&testValue = fixTest;
Or
typedef union{int32_t i, float f} intfloat;
intfloat tv;
tv.i = fixTest;
testValue = tv.f;
You get what is specified in subject, big integer is converted to float correctly. But I see you want to treat integer as a binary representation of float. Try this:
float testValue;
*(int32_t*)&testValue = fixTest;
Or
typedef union{int32_t i, float f} intfloat;
intfloat tv;
tv.i = fixTest;
testValue = tv.f;