Converting from Int to float

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Converting from Int to float

Jump to solution
1,549 Views
anze
Contributor I

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.

Labels (1)
0 Kudos
1 Solution
833 Views
kef
Specialist I

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;

View solution in original post

0 Kudos
1 Reply
834 Views
kef
Specialist I

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;

0 Kudos