Floating-Point Arithmetic and Rounding
You'll learn why computers can't always calculate decimal numbers exactly and why checking floating-point equality is dangerous.
The Core Problem
When you perform arithmetic with floating-point numbers, the computer often cannot represent the exact result. Just as you learned that IEEE 754 uses a fixed number of bits to approximate real numbers, calculations with those approximations compound the problem. The result must be rounded to fit back into the available precision.
Think of it like measuring with a ruler marked only in quarter-inches. If you add 3/8" + 3/8", you get 6/8" (or 3/4"), which can be marked exactly. But if you add 1/3" + 1/3", the true answer is 2/3"—which your ruler can't show precisely. You must round to the nearest mark, introducing error.
Why Exact Equality Fails
Consider this seemingly simple calculation:
0.1 + 0.2 = ?
You'd expect 0.3, but in binary floating-point, 0.1 cannot be represented exactly—it's a repeating fraction in binary, like 1/3 in decimal. The computer stores an approximation. When you add two approximations and round again, the result might be 0.30000000000000004.
This means checking result == 0.3 will fail, even though mathematically it should succeed. Instead, you must check if the result is close enough using a small tolerance value (epsilon).
Accumulating Errors
Rounding happens after every operation. In a long chain of calculations, these tiny errors can accumulate, causing results to drift significantly from the true mathematical answer.
Key Takeaway: Floating-point arithmetic introduces unavoidable rounding errors due to limited precision; never compare floating-point results using exact equality—always use a tolerance-based comparison instead.