Best afterOperator precedence: × before +
Modular arithmetic: wrapping around
Clocks don't count to 15 — they wrap back to 3. That wrap-around is exactly what the remainder operator % does, and it's the same idea behind fixed-width numbers overflowing. Watch the clock with and without it.
It's 10 o'clock. What hour shows on a 12-hour clock 5 hours later? (we want 3).
Forgetting to wrap the clock execution-derived · CPython
variables
state after line 1 runs
| variable | value |
|---|---|
start | 10 |
step 1 / 2
Click a line, drag the slider, or use the ← → keys.
What you are looking at
Watch clock: does it keep climbing, or wrap back around 12?
- Buggy —
clock = start + 5gives15— there's no 15 on a 12-hour clock. The code simply forgot to wrap. - Procedural —
clock = (start + 5) % 12gives3.%takes the remainder after dividing by 12, wrapping the value back into range.
Every state you see came from running the program under CPython's tracer at build time — see how GlassBox stays honest.