Exercise 1: Why 0.1 + 0.2 == 0.3 Is False — Possible Solution ==================================================================== THE ACTUAL VALUES INVOLVED ------------------------------ When a computer evaluates 0.1 + 0.2 in standard double-precision floating point, it does not compute the mathematically exact 0.3. Verified directly: 0.1 + 0.2 -> 0.30000000000000004440892... 0.3 -> 0.29999999999999998889777... These are two genuinely different stored numbers. Their difference is approximately 5.55 x 10^-17 - an extremely small gap, but not zero. WHY THE COMPARISON FAILS ------------------------------ The == operator checks for exact equality between the two stored bit patterns. Since 0.1 + 0.2 produces a value that is 0.30000000000000004... and the literal 0.3 is stored as 0.29999999999999998..., their underlying bit patterns are different. == correctly reports that they are different values - it isn't lying or malfunctioning, it's accurately comparing two numbers that genuinely aren't equal. WHY THIS ISN'T "FLOATING-POINT IS JUST IMPRECISE" ------------------------------ Saying "floating-point is imprecise" doesn't explain anything specific - it's true but vague. The precise explanation is: neither 0.1 nor 0.2 can be represented exactly in binary floating point in the first place (this is proven at the bit level in Chapter 2), so each one is already a tiny approximation before any addition even happens. Adding two approximations together doesn't magically cancel their errors out - it produces a new approximation whose error is a combination of both inputs' own rounding error. The 0.3 literal is ALSO its own separate approximation, stored with its own slightly different rounding. The bug isn't the addition; it's that three different approximations (0.1's, 0.2's, and 0.3's) don't happen to land on the exact same nearest representable value. WHY THIS WORKS AS AN ANSWER ------------------------------ The explanation names the two actual stored values (not just "it's imprecise"), states the actual numeric gap between them (5.55 x 10^-17), and explains specifically why == correctly reports inequality given those two different underlying values, rather than treating the comparison operator itself as the source of the problem.