Saturday, August 20, 2005

Carmacks number

News: Quake-3 source code has been officially released.
Interesting consequence: I come to know about Carmacks number.

In brief, this function was found in the source code of Quake-3 (in 2002, when they released the source code sans engine) to find (read: approximate) the inverse of the square root of x:

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
return x;
}

Its basically two iterations of the Newton method - the interesting line to note here is the value of the initial guess 0x5f3759df. This was used by one of the authors of Quake-3 and might not be the great great great Carmack himself but generally attributed to him. Isnt is amazing ... this number hack! Its a pretty unique number - so Google it to find more.

- d.

1 comment:

Siddhartha Saha said...

Sala, blog update koris na keno?

Post a Comment