sqrt函数的实现主要有三种方式:
1 二分法
2 牛顿法
3 卡马克方法
float InvSqrt(float x)
{
float xhalf = 0.5f*x;
int i = *(int*)&x; // get bits for floating VALUE
i = 0x5f3759df-(i>>1); // gives initial guess y0
x = *(float*)&i; // convert bits BACK to float
x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy
x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy
x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy
return 1/x;
}
卡马克方法的详细原理可查看维基百科
https://en.wikipedia.org/wiki/Fast_inverse_square_root
版权声明:本文为u014682691原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。