Fundamentals7 min readLast updated: Tue Mar 26 2024 00:00:00 GMT+0000 (Coordinated Universal Time)

Why 0.1 + 0.2 != 0.3

Open your browser console or a Python terminal and type:
0.1 + 0.2

The result will likely be: 0.30000000000000004

Why can't a multi-billion dollar computer do basic addition?

The Problem: Binary Fractions

Humans use Base-10 (Decimal).

  • 1/2 = 0.5 (Clean)
  • 1/5 = 0.2 (Clean)
  • 1/3 = 0.33333... (Repeating)

Computers use Base-2 (Binary).
In Binary, numbers like 0.5 are clean (0.1), but numbers like 0.1 or 0.2 are repeating fractions.

  • 0.1 in binary is 0.0001100110011...

IEEE 754 Standard

Computers have limited memory (usually 64 bits for a "Double" precision number). They cannot store an infinite repeating sequence. They have to cut it off eventually.

This tiny "rounding error" usually doesn't matter. But when you add two rounded numbers together (0.1 + 0.2), the errors stack up just enough to become visible.

How to fix it?

Never use Floating Point numbers for money.
Instead of storing $10.50, store 1050 cents (an Integer). Integers are always precise.