In this article, we'll talk about the "magic" constant 0x5f3759df, which lies at the heart of the elegant algorithmic trick for quick calculation of the inverse square root .

Here is the complete implementation of this algorithm:

```float FastInvSqrt(float x) { float xhalf = 0.5f * x;   int i = * (int *) & x; // represent the float bits as an integer   i = 0x5f3759df - (i >> 1); // what the hell is going on here ? x = *(float*)&i; x = x*(1.5f-(xhalf*x*x)); return x; }``` This code calculates some (fairly good) approximation for the formula

Today, this implementation is already well known, and it became so after the appearance in the code of the game Quake III Arena in 2005. Her creation was once attributed to John Carmack, but it turned out that roots go much further - to Ardent Computer , where in the mid-80s it was written by Greg Walsh. Specifically, the version of the code that is shown above (with funny comments) is really from the Quake code.
In this article we will try to understand this hack, mathematically derive this very constant and try to generalize this method to calculate arbitrary degrees from -1 to 1.

Yes, it will take a bit of math, but the school course will be more than enough.
xially 19 september 2017, 9:34