You would like to calculate a the inverse square root of a number quickly.
Solution
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
method.
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
Then copy & paste the compiled js code in to the JavaScript console of your
browser.
Note: You will need a browser which supports typed-arrays.