0x5f3759df

November 27, 2007 at 7:15 pm (Uncategorized) ()

The magic number that gives you the first approximation for the Newton Raphson method for finding the inverse square root of number.

The code is more than 15 years old but came to light when Quake 3D released its Doom III source code to public.

The code has been associated to John Carmack, Gary Tarolli and others but the origin of this number remains a mystery


float InvSqrt (float x){
float xhalf = 0.5f*x;
int i = *(int*)&x;
i = 0x5f3759df - (i>>1);
x = *(float*)&i;
x = x*(1.5f - xhalf*x*x);
return x;
}

Permalink Leave a Comment