Magical Square Root Implementation In Quake III by Carmack
codemaestro.com
Magical Square Root Implementation In Quake III by Carmack
1–10 of 19 posts
Re: Magical Square Root Implementation In Quake III by Carmack
#2First the implementation solves for the 'Inverse Square Root', not the square root. [1] Secondly the algorithm's author isn't actually known but the earliest person it can be traced too is Gary Tarolli [1].
[1] https://en.wikipedia.org/wiki/Fast_inverse_square_root
It really bothers me when people write something and their own sources differ from what their writing let alone wikipedia.
Re: Magical Square Root Implementation In Quake III by Carmack
#3It's worth reading, learning, and understanding, if only to be grateful that most of your life you'll never have to resort to such tricks. Sadly, that also means you'll never get the chance to experience the sheer joy of creating something like this.
Having said that, this article is actually really, really poor, and most of its value lies in the references provided by people in the comments. Many of the other submissions here on HN are much better.
Re: Magical Square Root Implementation In Quake III by Carmack
#4Re: Magical Square Root Implementation In Quake III by Carmack
#5Here's a much better article on the same topic: http://www.beyond3d.com/content/articles/8
Re: Magical Square Root Implementation In Quake III by Carmack
#6on about page 481 or so:
"The Newton step is a standard Newton-Raphson calculation for the reciprocal square root function (see Appendix B). Simply repeating this step reduces the relative error to the range 0 to -0.0000047. The optimal constant for this is 0x5F37599E."
Re: Magical Square Root Implementation In Quake III by Carmack
#7 *(foo*)
you're probably breaking the strict aliasing rule.Re: Magical Square Root Implementation In Quake III by Carmack
#8Re: Magical Square Root Implementation In Quake III by Carmack
#9Are there other weird number hacks like this one?
For example:
public static int hashCode(double a[])
{
if (a == null) return 0;
int result = 1;
for (double element : a) {
long bits = Double.doubleToLongBits(element);
result = 31 * result + (int)(bits ^ (bits >>> 32));
}
return result;
}Re: Magical Square Root Implementation In Quake III by Carmack
#10I wish the myriad articles about this interesting hack would bother to mention that it relies on undefined behaviour. Any time you find yourself doing this: *(foo*) you're probably breaking the strict aliasing rule.