Apex › Storage & databases

The fast inverse square root

The most famous 20 lines in game programming — Quake III's trick for computing 1/√x with no sqrt and no division. A float's raw bits, read as an integer, are almost exactly a scaled log₂(x); so a single integer subtraction from the magic constant 0x5f3759df computes x^(−1/2) in log space, and one Newton step polishes the ~3.4% guess to ~0.17%. Pick x and watch the bits become the answer.

More in Storage & databases