Why is multiplying cheaper than dividing?

后端 未结 3 1488
小鲜肉
小鲜肉 2021-02-15 16:03

I recently wrote a Vector 3 class, and I submitted my normalize() function for reviewal to a friend. He said it was good, but that I should multiply by the reciprocal where poss

3条回答
  •  -上瘾入骨i
    2021-02-15 16:23

    The CPU operation for (float) division is much more complicated than multiplication. The CPU has to do more. I am far from knowledgeable about hardware, but you can find a lot of info about common division implementation (based on newton-raphson algorithms, for example).

    I would also be careful about always using multiplication of the reciprocal instead of division to gain CPU performance: they may not give exactly the same results. This may or may not matter depending on your application.

提交回复
热议问题