Fast Inverse Square Root
You would like to calculate a the inverse square root of a number quickly.
Appearing in the Quake III Arena source code, this strange algorithm uses
integer operations along with a ‘magic number’ to calculate floating point
approximation values of inverse square roots.
In this CoffeeScript variant I supply the original classic, and newer optimal
32 bit magic numbers found by Chris Lomont. Also supplied is the 64-bit
sized magic number.
Another feature included is the ability to alter the level of precision.
This is done by controlling the number of iterations for performing Newton’s
Depending on the machine and level of precision this algorithm may still
provide performance increases over the classic.
To run this, compile the script with coffee:
coffee -c script.coffee
Note: You will need a browser which supports typed-arrays.
This code is in gist form here: