Floating-Point Math
Transcript
Made automatically from the video's sound, so it may contain mistakes. Click a line to play from there.
Welcome back. Okay, so in our last explainer, SO8, EO6, Distributed Consensus and Split Brain, we saw just how hard distributed servers struggle to agree on a shared truth across faulty networks.
Well, today we are kicking off a brand new arc, Deceptively Broken Primitives. We're zooming all the way down into a single CPU register to see what happens when even elementary decimal arithmetic completely bends our intuition. Right, let's just dive straight into it.
You ask a computer to do something literally as simple as it gets. You ask it to add 0.1 and 0.2. But instead of returning a nice clean 0.3, it spits out this monstrous anomaly. 0.3 followed by a ridiculous string of zeros ending in a 4.
Now, if you've ever written a script, you've almost certainly bumped into this and thought, wait, is my code broken? Well, actually, scratch that. I can assure you it's not.
The machine is doing exactly what it was built to do. So here is exactly how we're going to untangle this whole mess today. One, the universal math glitch. Two, inside the CPU register.
Three, real-world precision disasters. Four, the vibe coder trap. Five, the precision decision matrix. And finally, six, when floats actually win.
Section one, the universal math glitch, a multi-language anomaly. Whether you're writing in C, Python, Java, JavaScript, or Rust, evaluating 0.1 plus 0.2 results in that exact same trail of zeros ending in a 4. You see, this brilliantly illustrates how almost all modern platforms map floating point numbers to the IEEE 754 binary64 double precision format.
This isn't just some quirky bug in your favorite programming language, it's a fundamental reality of how hardware natively handles decimals. Section 2. Inside the CPU register: the anatomy of a float.
Now, what's really fascinating here is how William Cahan's 64-bit architecture breaks down. The layout packs integers derived from three distinct components. First up, a single sign bit, which is zero for positive, one for negative.
Second, an 11-bit biased exponent that spans absolute cosmic scales of magnitude. And third, 52 explicit mantissa bits plus one implicit leading bit. That gives us exactly 53 total bits of precision. The standard also specifies special cases, like subnormals and gradual underflow, distinct positive and negative zeros, and representations for infinity and not a number, which famously doesn't even equal itself.
But it's those 53 bits of precision where our problem actually lies. Here's the crucial point. A base 10 fraction, like 110th, literally cannot be represented exactly in binary. In base 2, 0.1 becomes this infinitely repeating fraction, 0.00110011, and so on forever.
It's like a repeating decimal in mass, right? But because the computer only has those 53 bits of precision to work with, the hardware brutally chops and rounds this infinite fraction at bit 53, using a rule called round ties to even.
So the computer never actually sees 0.1. It just sees the closest representable binary fraction, which happens to be very slightly larger than the true value. Section 3.
Real-world precision disasters, the macro catastrophes. Let's see how this builds into literal life or death scenarios when these microscopic round-off errors accumulate over time. In 1991, during Operation Desert Storm, a United States Patriot missile battery at Darran, Saudi Arabia, completely failed to track and intercept an incoming Scud missile.
Tragically, the Scud hit an army barracks, killing 28 Americans. A subsequent government accountability office report revealed the culprit. It was a software problem in the weapons control computer. The system tracked time by counting tenths of a second.
But, as we just saw, 0.1 cannot be represented exactly in binary. The system's 1970s era computer used a 24-bit register. By truncating that infinitely repeating binary representation of 0.1 to fit 24 bits, it introduced a microscopic inaccuracy.
But over a hundred hours of continuous operation, that tiny error accumulated, causing a drift of about 0.34 seconds. Now, a scud travels so incredibly fast that a 0.34 second tracking error meant the system's range gate shifted by roughly 687 meters.
The radar was looking in the completely wrong place, and the interception failed. Another absolutely catastrophic, though thankfully non-lethal example of accumulated error happened with the Vancouver Stock Exchange. In January 1982, they initialized their index at 1000.000.
Thousands of times a day, the index was updated and the new value was iteratively truncated to three decimal places. By simply dropping the remaining digits instead of rounding properly, the exchange was silently bleeding about 25 points a month.
In less than two years, the index plunged to 524.811. When they finally corrected the maths over a weekend in November 1983, the true value was revealed to be 1,098.892. Over half the index's value had just vanished into the ether of rounding errors.
It's a systemic failure that echoes other massive numerical disasters, like the infamous Ariane 5 rocket explosion caused by a float overflow. Section 4, the Vibe Coder Trap. AI and modern pitfalls.
Today we face a completely new threat. We call these vibe coder traps. AI coding assistants, often trained on tutorial shopping carts, might blindly replicate these dangerous hardware defaults.
An AI bot might confidently generate a calculation like price times quantity times 1 minus discount using standard binary floats. Or it might suggest creating SQL ledgers using float columns, or write naive checks expecting a balance to equal exactly zero, which totally fails when leftover fractions of a cent linger in the register.
You get corrupted ledgers, poisoned float literal constructors, and split formulas that literally invent or destroy pennies across multi-party invoices. And then there's the trap of the JSON parser. Take a look at this very specific number.
9,00719924574991. This is number.maxSafe integer in JavaScript, which is exactly 2 to the power of 53 minus 1. If you decide to use a 64-bit float for a massive database identifier, like a Snowflake ID generated by a modern distributed database, any number past this 9 quadrillion threshold cannot be represented precisely.
So when JSON.pass attempts to coerce that massive ID into a standard double precision float, it just silently truncates it, corrupting your data completely. Section 5, the precision decision matrix, engineering guardrails. To stop all of this, we need actionable engineering grade guardrails.
This decision matrix reveals the truth about types. If you're working with currency, you absolutely must never use raw floats. Instead, use integer minor units, like Stripe does, adhering to ISO 4217, where $10 is recorded as 1,000 cents.
Alternatively, use exact string initialized decimals, or Postgres's numeric 19.4 type. These exact types support bankers rounding, completely sidestepping the hardware glitch altogether.
And what about splitting payments? Say you need to divide 100 three ways. You can't just divide by three and trust the floating point. You must use a remainder preserving penny allocation technique, which was popularized by engineers like Martin Fowler and Matt Fomel.
You give $33.33 to each party, then you calculate the remainder, which is one cent, and allocate that single cent to the first party. That way, you never lose or gain a phantom cent. Section 6, when floats actually win.
The Yagney principle. But before you go ripping every single float out of your code base, remember the Yagney principle. You aren't gonna need it.
Exact decimal objects are mathematically safe, sure, but they can be a hundred times slower. Hardware floats are vastly faster, making them absolutely essential for non-exact domains.
If you are doing geospatial maths, audio digital signal processing, or training machine learning models using massive float 32 or B float 16 tensors, you really want that hardware acceleration. In these domains, blistering speed is absolutely critical, and we handle the microscopic approximations using tools like Cahan summation and math.is close checks.
So the next time you see a digital number on a screen, ask yourself: is this an exact truth? Or just the closest illusion the machine can render? Keep that distinction in mind as you build your systems.
Well, that wraps up our deep dive into floating point maths. Join us for our next explainer in S08E08, where we are going to dive into an equally chaotic world: string encoding and Unicode. Thanks for watching.