r/learnprogramming • • Sep 29 '25

TIL about Quake III's legendary "WTF?" code

This is a wild piece of optimization from Quake III Arena (1999):

float Q_rsqrt( float number )
{
    long i;
    float x2, y;
    const float threehalfs = 1.5F;

    x2 = number * 0.5F;
    y = number;
    i = * ( long * ) &y;                       
// evil floating point bit level hacking
    i = 0x5f3759df - ( i >> 1 );               
// what the fuck? 
    y = * ( float * ) &i;
    y = y * ( threehalfs - ( x2 * y * y ) );

    return y;
}

Those are the actual comments. It calculates inverse square roots 4x faster than normal by treating float bits as an integer and using a "magic number" (0x5F3759DF). Nobody knew who wrote it for years, turned out to be Greg Walsh from the late 1980s.

Modern CPUs have dedicated instructions now, but this remains one of the most elegant low-level hacks ever written.

https://en.wikipedia.org/wiki/Fast_inverse_square_root

1.5k Upvotes

131 comments sorted by

View all comments

Show parent comment

208

u/WasteKnowledge5318 Sep 29 '25

It does indeed approximate. Engineering is all about approximations and tolerances.

We can only ever get an `approximate` value of the area of a circle. :)

169

u/afineedge Sep 29 '25

Excuse me, but my country has a nearly infinite coastline thanks to this. 

0

u/[deleted] Sep 29 '25

[deleted] — view deleted comment

5

u/Bpollard85 Sep 29 '25

And I just learned of this term thanks to you! Thanks random redditer!

9

u/ElCuntIngles Sep 29 '25

You'll be seeing it everywhere for a while

48

u/sonofaresiii Sep 29 '25

No man we can get an exact area of a circle. It's the radius squared times pi. Exactly.

What we have to approximate is its expression as a decimal.

15

u/WasteKnowledge5318 Sep 29 '25

Sure, we can get the exact area: it's πr². Easy! Now if you want me to actually tell you what that number is... well, that's where things get approximate. The math is perfect; our number system just wasn't invited to the party.

31

u/grepTheForest Sep 29 '25

If you use base pi then it's not approximate.

Say the radius is 1. Then the area is 10.

12

u/XenophonSoulis Sep 29 '25

Good luck explaining that to a computer (they only understand base 2).

3

u/Xmaddog Sep 29 '25

Are you sure about that?

5

u/XenophonSoulis Sep 29 '25

Good luck finding one of those.

4

u/Xmaddog Sep 29 '25

Nothing stopping you from making one.

5

u/XenophonSoulis Sep 29 '25

Complete lack of interest does. I'd love to code in a C++ level ternary language, but for that to happen I'd have to make the Assembly first and then the language.

4

u/DeLoreansDontRust Sep 29 '25

It’s been 5 hours. Are you almost done?

→ More replies
→ More replies (3)
→ More replies (2)
→ More replies (3)

11

u/geon Sep 29 '25

pi is a number.

6

u/WasteKnowledge5318 Sep 29 '25

An irrational and transcendental one

11

u/McFestus Sep 29 '25

Sure. But not an approximate one.

2

u/StaysAwakeAllWeek Sep 29 '25

Go on then, enumerate pi without referring to itself

19

u/phlogistonical Sep 29 '25

Enumerate 1 without referring to itself

→ More replies (1)

4

u/Gositi Sep 29 '25

Sure, take the multiplicative inverse of this.

3

u/NSNick Sep 29 '25

4*arctan(1)

→ More replies (1)
→ More replies (1)
→ More replies (15)

2

u/florinandrei Sep 29 '25

I approximately agree with your comment, and I think it can be tolerated.

→ More replies (1)