ARTICLE AD BOX
Game developers didn’t person it easy successful nan 1990s. Because they had highly constricted computing power, they had to constitute their codification arsenic efficiently arsenic possible. Consider nan first-person shooter Quake III Arena, usually called Quake 3, for example: players navigated a three-dimensional world, truthful nan programmers had to find nan cleverest ways to grip 3D graphics and nan associated calculations.
Quake 3 released successful 1999 and is considered 1 of nan champion machine games of its time. It had a lasting effect connected nan industry. This bequest wasn’t truthful overmuch owed to nan story, but alternatively because Quake 3 was 1 of nan first multiplayer first-person shooters. Players could link their computers via web cables aliases nan net to compete successful existent time.
The game’s codification near a people too. It included an highly businesslike algorithm that still amazes experts and sparks curiosity among scientists.
On supporting subject journalism
If you're enjoying this article, see supporting our award-winning publicity by subscribing. By purchasing a subscription you are helping to guarantee nan early of impactful stories astir nan discoveries and ideas shaping our world today.
A unusual code
To fig retired nan orientations of objects, characters aliases different players successful three-dimensional abstraction mathematically, you create a vector, which is fundamentally an arrow that shows direction. To comparison vectors, they request to beryllium normalized to nan aforesaid length, truthful you person to standard them accordingly. And that’s wherever a tricky calculation comes up: nan inverse quadrate root, which is 1 divided by nan quadrate guidelines of a number.
If I asked you to cipher nan inverse quadrate guidelines of 26 without a calculator, you’d astir apt beryllium stuck for a while—and honestly, truthful would I. Back successful nan 1990s computers faced nan aforesaid challenge. Although they could crunch nan numbers, nan process demanded a batch of processing power—which tin mean nan calculation takes a batch of time. One problem was nan quadrate guidelines itself; different was nan division. That’s why nan Quake 3 programmers hunted for a amended measurement to find this inverse quadrate root. And indeed, their source code revealed an ingenious solution.
What’s fascinating is that nan developers ne'er advertised their trick. If Quake 3’s root codification hadn’t gone unfastened source, their method mightiness person stayed hidden forever. But erstwhile it was released, funny enthusiasts took notice. When they discovered nan codification snippet for calculating nan inverse quadrate root, they were baffled—it was difficult to follow, and nan developers’ accompanying comments weren’t peculiarly helpful. But gradually group figured retired really nan codification worked.
Today location are many tutorials that guideline you step-by-step done nan programme code. These walkthroughs utilization typical features of nan C programming language. For example, numbers are stored successful machine locations called representation addresses, which are past manipulated. This is simply a clever measurement to debar computationally intensive operations for illustration division. “Think of it for illustration putting nan incorrect tag connected thing astatine nan shop and it convincing nan worker but present it is C we fool,” explained machine intelligence Daniel Harrington from nan University of Toronto successful a presentation.
From a mathematical perspective, nan codification is easy explained. To find nan inverse quadrate root, you first make a conjecture astatine nan solution (which is mostly incorrect) and past refine that estimate done a group procedure. In this way, it gradually reaches amended solutions.
None of this is groundbreaking aliases new. What is impressive, however, is that usually 4 to 5 iterations of nan process are needed earlier nan consequence is adjacent capable to an existent solution. This process requires a batch of computing power. In Quake 3, nan starting value—that is, nan estimated number utilized successful nan first measurement of nan process—was chosen truthful cleverly that only a azygous optimization measurement is necessary.
Seeking a magic number
The optimization steps correspond to nan alleged Newton-Raphson method, which approximates nan values astatine which a usability produces an output of 0, aliases nan guidelines of functions, complete galore iterations. This whitethorn sound counterintuitive astatine first, since 1 wants to cipher nan inverse quadrate guidelines and not conscionable immoderate zero. But nan programmers employment a trick: they specify nan usability to beryllium approximated arsenic nan quality betwixt nan first estimate worth and nan existent result. Through Newton-Raphson’s method, nan correction frankincense becomes progressively smaller, allowing 1 to get ever person to nan nonstop solution.
To deliberation this through, ideate you want to cipher nan inverse quadrate guidelines of 2.5. The algorithm starts pinch a definite guess: let’s opportunity 3.1. To find nan quality from nan existent solution, you quadrate nan first worth and disagreement 1 by nan result. If 3.1 were genuinely nan inverse quadrate guidelines of 2.5, past 1 divided by 3.1 squared would beryllium 2.5. The existent consequence is 0.1. The quality is truthful 2.4.
The Newton-Raphson method reduces this quality complete each loop truthful that you gradually get person to nan nonstop value. Typically 4 to 5 specified steps are needed to get astatine a reliable result. Yet Quake 3 reduced iterations significantly.
The cardinal is successful really nan starting worth for nan Newton steps is calculated. The method’s algorithm fundamentally operates successful 3 steps:
Take nan fixed number whose inverse quadrate guidelines is to beryllium calculated and person it into a corresponding representation reside (a location successful nan computer’s stored data).
This worth is halved and subtracted from nan hexadecimal worth 0x5f3759df. This is nan starting worth for nan Newton method.
Next, execute a Newton step.
Particularly mysterious is nan cryptic drawstring 0x5f3759df, which has since gone down successful machine subject history arsenic nan “magic number.” It is nan logic why only 1 loop is basal to get an approximate solution for nan inverse quadrate guidelines that produces an correction of astatine astir 0.175 percent.
As soon arsenic nan programme codification was disposable arsenic unfastened source, experts puzzled complete nan root of that magic number. In a method insubstantial published successful 2003, machine intelligence Chris Lomont wrote: “Where does this worth travel from, and why does nan codification work?”
The hexadecimal number 0x5f3759df corresponds to 1,597,463,007 successful decimal notation. By breaking down nan individual steps of nan programme code, Lomont realized that he could get 1,597,463,007 done definite calculations. To make this mathematics simpler, here’s 1 measurement to correspond nan calculation involved:

The values 3⁄2, 223 and 127 travel from converting nan number representations into C. But 0.0450465’s root is little obvious.
Lomont mathematically investigated which worth yields an optimal consequence for different inputs. In different words: Which starting worth champion approximates nan inverse quadrate guidelines and should truthful lead to nan smallest error? He arrived astatine a worth of 1,597,465,647, which is approximately:

This corresponds to nan values recovered successful nan Quake 3 root code. The consequence is rather adjacent to nan values recovered there.
When Lomont compared his results pinch those of nan original, he encountered a surprise. In 2 steps of nan Newton-Raphson method, his calculated changeless really worked better: nan maximum imaginable correction was smaller than pinch nan worth successful nan original code. "Yet surprisingly, aft 1 Newton iteration, it has a higher maximal comparative error," Lomont writes. "Which again raises nan question: really was nan original codification changeless derived?"
In his calculation, nan machine intelligence had only considered which number would theoretically output nan champion imaginable value, neglecting nan number of Newton steps. In hunt of a amended constant, Lomont repeated his calculation and optimized for nan champion imaginable solution for a azygous Newton step. He arrived astatine a worth of 1,597,463,174, which is approximately:

When he put this consequence to a applicable test, it really yielded somewhat amended results than nan magic number successful nan Quake 3 code.
Lomont noted successful his insubstantial that since some constants are approximations, either is simply a bully action successful practice. He added that he hoped to meet nan original writer of nan changeless to study really they derived nan magic number.
Online communities began a relentless hunt for this enigma person. Particularly dedicated to this effort was computer intelligence Rys Sommefeldt, who first contacted John Carmack, nan lead developer of Quake 3. Carmack was unsure of who coded this snippet and could only connection guesses, however.
Sommefeldt contacted immoderate of nan astir salient developers of nan 1990s, who each suggested different imaginable authors without claiming authorship for themselves. It now appears that Greg Walsh, who worked for nan machine shaper Ardent Computer successful nan precocious 1980s, introduced nan magic number into nan inverse quadrate guidelines algorithm. It past recovered its measurement into nan Quake 3 algorithm via respective different individuals. But precisely really nan magic number was wished remains unclear to this day.
That’s not a peculiarly satisfying conclusion. Nevertheless, nan communicative of nan Quake 3 code—or astatine slightest nan portion that revolves astir nan inverse quadrate root—is highly fascinating. It’s astonishing really overmuch effort and brainpower went into businesslike package programming backmost then—a inclination that’s often overlooked coming acknowledgment to existent computing power.
This article primitively appeared successful Spektrum der Wissenschaft and was reproduced pinch permission.
5 bulan yang lalu
English (US) ·
Indonesian (ID) ·