What is happening here in pow function?

前端 未结 5 554
北海茫月
北海茫月 2020-12-01 23:53

I have seen various answer here that depicts Strange behavior of pow function in C.
But I Have something different to ask here.

In the below code I

5条回答
  •  广开言路
    2020-12-02 00:35

    As others have mentioned, Code 2 returns 99 due to floating point truncation. The reason why Code 1 returns a different and correct answer is because of a libc optimization.

    When the power is a small positive integer, it is more efficient to perform the operation as repeated multiplication. The simpler path removes roundoff. Since this is inlined you don't see function calls being made.

提交回复
热议问题