[问卦] 快速平方根倒数算法 也太神了8?

楼主: dzwei (Cout<< *p << \n ;)   2024-04-12 13:48:10
如此扣:
float Q_rsqrt(float number)
{
long i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
// evil floating point bit level hacking
i = * ( long * ) &y;
i = 0x5f3759df - ( i >> 1 ); // what the fuck?
y = * ( float * ) &i;
// 1st iteration
y = y * ( threehalfs - ( x2 * y * y ) );
// 2nd iteration, this can be removed
// y = y * ( threehalfs - ( x2 * y * y ) );
return y;
}
说明影片:
https://youtu.be/g1r3iLejTw0?si=RQjiaRF6lFOGeC0e
return前最后两行看的出来是牛顿法
但那个WTF那行看不懂
刚刚看了YT
才知道那句WTF
原来有那么大的意义
一次叠代误差就可以<1%
这个算法被用在游戏
想出这种计算的方式的人
也太神了吧

Links booklink

Contact Us: admin [ a t ] ucptt.com