Why 0.1 + 0.2 Doesn't Equal 0.3 — Floating Point Errors Explained
Understand why computers produce floating point precision errors, how IEEE 754 representation causes rounding, and practical ways to handle it in code.
Introduction
Open any programming language console and type 0.1 + 0.2. You would expect 0.3, but instead you get something like 0.30000000000000004. This is not a bug in the language — it is a fundamental consequence of how computers represent decimal numbers in binary. Understanding why this happens makes you a better programmer and helps you avoid costly mistakes in financial or scientific software.
🔧 See it in action: IEEE 754 Converter — visualize how decimals are stored in 32-bit and 64-bit binary floating point.
Why Does This Happen?
Computers store numbers in binary (base 2). Just as 1/3 = 0.333… repeats infinitely in decimal, many simple decimals repeat infinitely in binary:
- 0.1 in binary = 0.0001100110011001100… (repeating)
- 0.2 in binary = 0.001100110011001100… (repeating)
Since computers have finite memory (64 bits for a double), these infinite sequences must be truncated. The truncation introduces a tiny rounding error. When you add two already-rounded numbers, the error accumulates.
The Math Behind It
Step 1: Convert 0.1 to Binary
Multiply by 2 repeatedly:
- 0.1 × 2 = 0.2 → 0
- 0.2 × 2 = 0.4 → 0
- 0.4 × 2 = 0.8 → 0
- 0.8 × 2 = 1.6 → 1
- 0.6 × 2 = 1.2 → 1
- 0.2 × 2 = 0.4 → 0 (cycle repeats)
Result: 0.00011001100110011… (infinite repeating pattern “0011”)
Step 2: Store in 64-bit IEEE 754
A 64-bit double has 52 mantissa bits. The infinite expansion is rounded to fit:
Stored value of 0.1 ≈ 0.1000000000000000055511151231257827021181583404541015625
That’s extremely close to 0.1 — but not exactly 0.1.
Step 3: Add Two Imprecise Values
When you add the stored 0.1 and stored 0.2, you get a result that is extremely close to 0.3 but has a tiny error in the last few bits, resulting in:
0.30000000000000004 (in most languages)
Which Numbers Are Exact?
Numbers that can be expressed as a finite sum of negative powers of 2 are stored exactly:
| Decimal | Binary | Exact? |
|---|---|---|
| 0.5 | 0.1 | ✅ Yes (2⁻¹) |
| 0.25 | 0.01 | ✅ Yes (2⁻²) |
| 0.75 | 0.11 | ✅ Yes (2⁻¹ + 2⁻²) |
| 0.1 | 0.000110011… | ❌ No (infinite) |
| 0.2 | 0.00110011… | ❌ No (infinite) |
| 0.3 | 0.01001100… | ❌ No (infinite) |
Rule: Fractions with denominators that are powers of 2 (1/2, 1/4, 1/8, etc.) are exact. Everything else may have error.
Code Examples
JavaScript
0.1 + 0.2 // 0.30000000000000004
0.1 + 0.2 === 0.3 // false
Python
0.1 + 0.2 // 0.30000000000000004
round(0.1 + 0.2, 1) // 0.3 (workaround)
Java
System.out.println(0.1 + 0.2); // 0.30000000000000004
When Does This Matter?
| Domain | Risk Level | Example Problem |
|---|---|---|
| Financial calculations | High | Rounding errors accumulate over millions of transactions |
| Scientific simulations | High | Small errors compound over many iterations |
| Game physics | Medium | Object positions drift slightly |
| UI display | Low | Show 2 decimal places and it looks fine |
How to Handle Floating Point Errors
1. Use Integer Arithmetic for Money
Store amounts in smallest unit (paise/cents):
// Bad: 19.99 + 0.01 might not equal 20.00
// Good: 1999 + 1 = 2000 (then divide by 100 for display)
2. Use Epsilon Comparison
Instead of a === b, use:
Math.abs(a - b) < Number.EPSILON
3. Use Decimal/BigDecimal Libraries
- Python:
decimal.Decimal('0.1') + decimal.Decimal('0.2')→ exact 0.3 - Java:
BigDecimalclass - JavaScript:
decimal.jsorbig.jslibraries
4. Round for Display
(0.1 + 0.2).toFixed(1) // "0.3"
Frequently Asked Questions
Is this a bug in JavaScript/Python/Java?
No. It is a fundamental property of IEEE 754 binary floating point representation used by all modern processors. Every language that uses hardware floats has this behavior.
Why not use base-10 floating point?
Hardware (CPU) is designed for binary. Base-10 arithmetic is slower because it requires software emulation. For most use cases, binary float is fast enough and the tiny error doesn’t matter.
Does this affect integer arithmetic?
No. Integers (whole numbers) within the safe range (up to 2⁵³ for JavaScript) are stored exactly in floating point. The problem only occurs with fractions.
How do banks handle this?
Banks typically use fixed-point decimal arithmetic (storing amounts as integers in paise/cents) or specialized decimal data types that avoid binary float entirely.
Can I see the exact binary representation of 0.1?
Yes — use our IEEE 754 Converter to enter 0.1 and see the exact 64-bit binary representation, including the sign, exponent, and mantissa bits.
Related Calculators
Related Articles
Share this article
Learn Faster with Numverto
Explore free number system converters, binary tools, EMI calculators, GST calculators, and educational guides.
About Numverto
Numverto Editorial Team
Numverto publishes educational content about number systems, computer science concepts, binary arithmetic, financial calculations, EMI formulas, GST calculations, and practical learning resources for students and professionals.
Article Metadata
Tags: ieee754, floating point, programming, binary
Last Updated: June 2026
Related Calculators
Related Articles
19 June 2026
Bits, Bytes, KB, MB, GB — Computer Memory Units Explained
Understand computer memory units from bits to terabytes. Learn binary vs decimal prefixes, conversion formulas, and real-world storage examples.
Read article →17 June 2026
What is 1's Complement and 2's Complement in Binary?
Learn 1's complement and 2's complement with simple explanations, step-by-step examples, and practice problems for BCA/BTech exams.
Read article →16 June 2026
ASCII Table — Complete Character Code Reference
Complete ASCII table with decimal, hexadecimal, and binary codes for all 128 characters. Includes printable characters, control codes, and practical usage examples.
Read article →