作者:
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%
這個算法被用在遊戲
想出這種計算的方式的人
也太神了吧