long int to float

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

long int to float

Jump to solution
1,326 Views
juancarlossuare
Contributor III

Hello.

¿How to convert from long int to float?

Labels (1)
0 Kudos
Reply
1 Solution
1,063 Views
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.


View solution in original post

0 Kudos
Reply
5 Replies
1,063 Views
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 Kudos
Reply
1,063 Views
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 Kudos
Reply
1,064 Views
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 Kudos
Reply
1,063 Views
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 Kudos
Reply
1,063 Views
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 Kudos
Reply