strange result on floating point calculation


Hello,
I'm currently experiencing a problem with Keil ARM compiler on a STM32 target with hw FP unit. I can not explain the reason for this and I am looking forward to any explanation and possibility to avoid if possible.
Please find below a code snippet which shows the problem.

The first calculation gives a slightly different result to the two other ones. Although all values involved as well as the result should be lossless representable as a float number, result1 is slightly inprecise.


volatile u32   million = 1000000;

volatile float thousand = 1000.0f;

volatile float result1, result2, result3;

 

result1 = (float)million / 1000.0f;        // -> 1000.00006

result2 = (float)1000000 / 1000.0f;        // -> 1000

result3 = (float)million / thousand;       // -> 1000

Parents Reply Children
No data