Hi, I have a problem when I tried to convert the data type. the processor is MCF5235CVM150 and the compiler is diab 5.2 from windriver (vxworks5.5.1). I have written a very simple function to test the type conversion. But the results were not as expected.
The issue is the rounding you can't really avoid. Not only can (most) integer numbers not be represented exactly in a 32 bit floating point value, also most *real* numbers cannot be represented, either. Consider your number:
This is converted to 32 bit floating point as:
+1 .11111111111111110010101 x 2^30
If you look at the floating point numbers in this area (the density of floating point numbers is not uniform), you will see:
4EFFFF94 = 2147469824.00
4EFFFF95 = 2147469952.00
4EFFFF96 = 2147470080.00
So you can see that for the number you requested (2147470000.00) the compiler had to choose the nearest floating point value (based on its rounding algorithm), and it chose 4EFFFF95. But once you tried to print it back out, it no longer knew what you requested -- only the real number -- so it printed it out exactly as 2147469952.00.
There are floating point calculators on the web which allow you to do this, such as:
I might not understand exactly what problem you are having, and which behavior you don't like...
A 4 byte float has less than 4 bytes for the mantissa, so it can't represent (most) 4 byte integers exactly, so rounding (up or down) occurs.
And when you convert from float to back to integer, the floating point value may not be representable in the integer's width at all.
(In your second case, you would have needed a 33 bit signed integer to represent the value. A 32 bit signed integer can represent the values from +2^31-1 to -2^31. If you had used an unsigned integer there (and a %u) you probably would have been OK, but you still would have had the rounding in the floating point value.)