sqrt(x) is known to be slow to compute. Gaming industry has devised a famous and mysterious fast inverse square root function to calculate a fast approximation of 1/sqrt(x). The page describes the clever mathematics of representing numbers in computers and includes an interactive simulation to visualize and clarify some fudging of constants to improve the approximation. The famous fast inverse square root function is some mysterious code not written by programming legend John Carmack site
graph
not Carmack ![]()
// Code from Quake 3 Arena. 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; // what the fuck? i = 0x5f3759df - ( i >> 1 ); 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; }