Back to the blog
Gaming

0x5F3759DF: The Magic Number That Made 3D Possible

February 15, 2026 6 min read

Illustration generated with Google Flow (Nano Banana Pro).
Illustration generated with Google Flow (Nano Banana Pro).

Somewhere in the source code of Quake III Arena, the 1999 shooter that taught a generation what 125 frames per second felt like, there is a function. It's twenty-odd lines long, it calculates a perfectly ordinary piece of math, and right in the middle of it sits a comment that has since become folklore: // what the f***?. Even the programmers who shipped it weren't entirely sure why it worked. The trick they were pulling off — computing one over the square root of a number, faster than the hardware of the day could do it honestly — is one of the most beautiful hacks in the history of code. And at its heart is a single, almost magical, hexadecimal number: 0x5F3759DF.

A frame from the open-source shooter OpenArena, a descendant of the id Tech 3 engine that powered Quake III — Credit: VaLouille / Wikimedia Commons (GPLv2+)
A frame from the open-source shooter OpenArena, a descendant of the id Tech 3 engine that powered Quake III — Credit: VaLouille / Wikimedia Commons (GPLv2+)

Why a game needs to divide by a square root all day

Three-dimensional graphics are, underneath the explosions, an enormous amount of geometry. To know how a surface catches the light, how a wall reflects, whether an enemy is in front of you or behind, the engine constantly works with normalized vectors — little arrows of length exactly one, pointing in a direction. Turning any arrow into a unit-length arrow means dividing each of its components by the arrow's length, and length is a square root. So the engine needs 1 / √x, the inverse square root, millions of times per second.

The honest way to compute that on a 1999 CPU was brutally slow. Square roots and divisions were among the most expensive operations a processor could do — the kind of thing that, repeated millions of times a frame, would drag your buttery shooter down to a slideshow. So the question facing the engine programmers wasn't how do we get the exact answer — it was how close can we get, fast, and good enough that nobody notices.

The bit trick that shouldn't work

Here's the sleight of hand. A floating-point number — the way computers store decimals — is just a pattern of bits: a sign, an exponent, and a fraction. The clever insight is that this layout secretly encodes a logarithm. The exponent bits are essentially the whole-number part of log₂ of your number, and the fraction bits approximate the rest.

And inverse square root, in the world of logarithms, is almost trivial: taking 1/√x means multiplying the logarithm by −½. So the algorithm does something that looks completely insane. It takes the floating-point number, reinterprets its raw bits as an ordinary integer (no conversion, just "read the same memory as if it were a different type"), and then computes, roughly, magic − (i >> 1). The shift-right halves the value; subtracting from the magic constant flips the sign and adds the correction. Read those integer bits back as a float, and — astonishingly — you've got a very good guess at 1/√x. One illegal-looking line of bit-fiddling has replaced a square root and a division.

Source code on screen — the kind of bit-level manipulation that makes the fast inverse square root look like sorcery — Credit: Jakub Żerdzicki / Unsplash
Source code on screen — the kind of bit-level manipulation that makes the fast inverse square root look like sorcery — Credit: Jakub Żerdzicki / Unsplash

What 0x5F3759DF actually is

That magic number isn't random, and it isn't a typo. It's the constant that makes the whole logarithm trick line up. When you halve and negate those exponent bits, you introduce a small systematic error, and 0x5F3759DF is tuned to keep that error as balanced — as small in the worst case — as possible. It is, in a real sense, the single best-chosen integer in this corner of computer graphics.

The guess it produces is good, but not perfect, so the code finishes with one pass of Newton's method — a centuries-old technique for sharpening an approximation. A single iteration takes the rough answer and refines it to within about 0.17% of the true value. That was more than precise enough for lighting a rocket-jump in a deathmatch, and the whole thing ran roughly four times faster than the straightforward calculation. (A second Newton pass would tighten it further, but why pay for accuracy nobody can see?)

The mystery of who wrote it

For years, the legend credited John Carmack, id Software's famous lead programmer, with conjuring 0x5F3759DF. It was a tidy story — genius coder, genius hack — but it wasn't true. Carmack shipped the code; he didn't invent the number.

A vintage arcade machine glowing with neon — the era of squeezed-cycle wizardry that made real-time 3D possible — Credit: Brunno Tozzo / Unsplash
A vintage arcade machine glowing with neon — the era of squeezed-cycle wizardry that made real-time 3D possible — Credit: Brunno Tozzo / Unsplash

When id released the Quake III source at QuakeCon in 2005, the puzzle only deepened, and curious programmers went digging. The trail, traced largely by a 2006 investigation, led back through the chip company Ardent Computer to a programmer named Greg Walsh, who devised the famous constant in the late 1980s. Walsh had picked up the underlying bit-fiddling idea from the mathematician Cleve Moler (the creator of MATLAB), who in turn had seen it in an unpublished 1986 paper by William Kahan and K. C. Ng at Berkeley — Kahan being the man who literally co-wrote the standard for how computers handle floating-point numbers in the first place. The hack that felt like a lucky accident was actually the distilled cleverness of some of the deepest numerical minds of the era, passed hand to hand for over a decade before it ever made a rocket fly.

The kicker

The final twist is almost poetic. A few years after the code went public, mathematicians sat down and searched for the truly optimal magic number — and they found a slightly better one, 0x5F375A86. The legendary constant, the one carved into programming lore, the one with the bewildered comment beside it, was never even quite the best choice. It was just close enough, fast enough, and shipped — which, in the end, is exactly how a game gets made.

A project like this one?

I design and deploy products like this. Let's talk.

Let's talk