MATHEMATICS

Kamis, 03 Maret 2011

M373 applied to computer games

The fact that computer games are computationally intensive forces game programmers to invent tricks to do their calculations in a minimal number of steps. A typical M373 optimization problem. It is much faster to calculate $\frac{1}{\sqrt{x}}$ using Newton approximation.

From the source code of Quake.
float InvSqrt(float x){
float xhalf = 0.5f * x;
int i = *(int*)&x; // store floating-point bits in integer
i = 0x5f3759d5 - (i >> 1); // initial guess for Newton's method
x = *(float*)&i; // convert new bits into float
x = x*(1.5f - xhalf*x*x); // One round of Newton's method
return x;
}

Link: Understanding Quake's fast inverse-square-root

Tidak ada komentar:

Posting Komentar