Colin Bootle - social media archive

If you like maths, what's ...

Colin Bootle 26/10/2020 11:31:04
If you like maths, what's the fastest way to calculate 1 ⁄ √x, to a reasonable approximation? Used in computer games to calculate reflections etc.

Answer: treat a floating point number as an integer, shift to the right (typically one CPU cycle) and subtract from a magic number (one more CPU cycle):

i = 0x5f3759df - ( i >> 1 );

Fast inverse square root - Wikipedia

Fast inverse square root, sometimes referred to as Fast InvSqrt() or by the hexadecimal constant 0x5F3759DF, is an algorithm that estimates ​1⁄√x, the reciprocal (or multiplicative inverse) of the square root of a 32-bit floating-point number x in IEEE 754 floating-point format. This operation...