If a float is just a whole number times a power of two, why does adding 0.1 ten times miss 1.0 — and what do you write instead of `==`?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
A float is stored as a significant digit together with a power of two. The pair
(125, -2) means significant digit 125 and exponent -2, and its real value is the
significant digit scaled by two raised to that exponent. Using the two values given
below, compute that base-10 value and assign it to value.
(Hint: ** is Python's exponent operator, so 2 ** -2 is 0.25.)
The (significant digit, power of 2) pair
Work (1, 1), (1, -1) and (125, -2) through both conversions, turning the significant digit into binary and shifting the point by the exponent to get 2.0, 0.5 and 31.25.
Truncation at 32 or 64 bits
Record that a binary fraction longer than the available 32 slots is cut off there, leaving 0.1 stored only approximately, with an error around 2**-32, roughly 2 * 10**-10.
Accumulated round-off and the epsilon rule
Track the 2**-32 error growing across the ten increments of the loop up to the 2**-16 slot, then state the rule: test floats within a small epsilon, never with ==.