Converting from Int to float

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Converting from Int to float

ソリューションへジャンプ
1,622件の閲覧回数
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.

ラベル(1)
0 件の賞賛
1 解決策
906件の閲覧回数
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 件の賞賛
1 返信
907件の閲覧回数
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 件の賞賛