Differences in rounded result when calling pow()

后端 未结 4 1758
无人及你
无人及你 2020-12-07 04:28

OK, I know that there was many question about pow function and casting it\'s result to int, but I couldn\'t find answer to this a bit specific question.

OK, this is

4条回答
  •  感情败类
    2020-12-07 04:56

    I'm fairly sure this can be explained by "intermediate rounding" and the fact that pow is not simply looping around j times multiplying by i, but calculating using exp(log(i)*j) as a floating point calculation. Intermediate rounding may well convert 24.999999999996 into 25.000000000 - even arbitrary storing and reloading of the value may cause differences in this sort of behaviuor, so depending on how the code is generated, it may make a difference to the exact result.

    And of course, in some cases, the compiler may even "know" what pow actually achieves, and replace the calculation with a constant result.

提交回复
热议问题