版权声明:博主瞎写,随便看看 https://blog.csdn.net/LAN74__/article/details/56008986
在网上无意浏览到一个神奇的小函数,开平方跟的,感到。。。。很奇妙。。码过来
float InvSqrt(float x)
{
float xhalf = 0.5f*x;
int i = *(int*)&x; // get bits for floating VALUE
i = 0x5f375a86- (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
return x;
}
这个函数效率据说要比sqrt高好多,是不是很神奇!(尤其是这一行: i = 0x5f375a86- (i>>1);)感到这好像就是玄学问题。。。
有兴趣的朋友可以看看这篇文章
http://kb.cnblogs.com/page/189867/