long int to float

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

long int to float

跳至解决方案
2,193 次查看
juancarlossuare
Contributor III

Hello.

¿How to convert from long int to float?

标签 (1)
0 项奖励
回复
1 解答
1,930 次查看
juancarlossuare
Contributor III

We can use type cast to convert long int to float. For example:

volatile long int l_val=5;

volatile float f_result,f_val=1.23;

f_result=(float)l_val/f_val;

In above code " l_val " computes as float.


在原帖中查看解决方案

0 项奖励
回复
5 回复数
1,930 次查看
Monica
Senior Contributor III

Juan, I invite you to mark the best answer as correct, this way other people experiencing the same problem can identify the solution.

If the answers here have not helped, let us know!

Regards!

0 项奖励
回复
1,930 次查看
juancarlossuare
Contributor III

Hello Monica.

This answer was given in a service request of freescale.

We can use type cast to convert long int to float. For example:

volatile long int l_val=5;

volatile float f_result,f_val=1.23;

f_result=(float)l_val/f_val;

In above code " l_val " computes as float.


0 项奖励
回复
1,931 次查看
juancarlossuare
Contributor III

We can use type cast to convert long int to float. For example:

volatile long int l_val=5;

volatile float f_result,f_val=1.23;

f_result=(float)l_val/f_val;

In above code " l_val " computes as float.


0 项奖励
回复
1,930 次查看
bigmac
Specialist III

Hello,

You might want to see if you get approximately the correct value when I_val is initialised with a value that exceeds the 16-bit  numeric range, e.g. long int I_val = 123456L;  Of course, the conversion to float will usually result in some loss of precision, maybe with accuracy limited to about 5 digits.

Regards,

Mac


0 项奖励
回复
1,930 次查看
kef
Specialist I

Mac,

conversion from int to float is exact provided integer can fit FP mantissa. For single precision float, with its 24bit mantissa, no precision is lost converting long int's in the range -16777215 to +16777215.

0 项奖励
回复