Workbench / Household Numbers: the Mathematics Capstone
S3 · B1·03
Totals You Can Trust
Stage 3, after chapter 3: a year of household spending totalled by month, checked by an estimate, by casting out nines and by the months adding up to the year; then the same year added in floating point, where the total stops being exactly right and rounding hides it.
Stage 3: totals you can trust
The brief
What you'll build. A year of everyday spending (milk, vegetables, tea and snacks, transport) totalled by month and for the year, with three checks on every total: the months must add up to the year exactly, an estimate (each amount rounded to the nearest rupee) must be close, and casting out nines must agree. Then you'll add the same amounts as floating-point rupees and see what changes.
Why a real household needs this. A year of small payments is where errors hide: a mistyped amount, a payment entered twice, a month missed. And many budgeting apps and spreadsheets store amounts as floating-point numbers; you'll see exactly what that does.
Why now. Chapter 3 gives column addition and its checks, explains what each check can and can't see, and shows floating-point sums drifting.
What you'll have at the end. totals.py, monthly and yearly totals for your own year (or the generated one), each check's result, and a note on which kinds of error each check would have missed.
Theory to own
- Partition. Why must the twelve monthly totals add up to the yearly total exactly? What would it mean if they didn't? (Chapter: the properties of addition; stage 1's invariant.)
- The estimate's error bound. Rounding each amount to the nearest rupee moves it by at most 50 paise. With 1,000 payments, how far can the estimated total be from the exact one, at most? Why is it usually much closer? (Chapter: "Estimate first".)
- Casting out nines. Which error in a yearly total would casting out nines catch, and which would it miss? (Chapter: "Casting out nines"; exercises, P3.)
- Floating point. Why would amounts in steps of 25 paise add exactly as floats, but amounts in steps of 10 paise not? (Chapter: lab, experiment 4; chapter 7 proves which fractions are exact in binary.)
Build it
Step 1: by hand. Take one month of your ledger (stage 1). Add it by columns, with carries, then check it three ways: re-add in a different order, estimate (round each item), and cast out nines. Note how long each check took.
Step 2: a year in code. If you don't have a year of data, generate one, as here. Amounts are whole paise in 10-paisa steps, like real prices:
"""Household numbers, stage 3: a year of totals, checked three ways, and what floats would have done."""
import random
from datetime import date, timedelta
random.seed(2026)
SPEND = {"milk": (3200, 3800), "vegetables": (4000, 25000), "tea and snacks": (1000, 6000), "transport": (2000, 30000)}
year = [] # (date, category, paise)
d = date(2026, 1, 1)
while d.year == 2026:
for cat, (lo, hi) in SPEND.items():
if cat == "milk" or random.random() < 0.6:
year.append((d, cat, random.randrange(lo, hi + 1, 10))) # amounts in 10-paisa steps
d += timedelta(days=1)
by_month = {}
for d, cat, p in year:
by_month[d.month] = by_month.get(d.month, 0) + p
total = sum(p for _, _, p in year)
assert total == sum(by_month.values()) # months partition the year
def rupees(p):
return f"₹{p // 100:,}.{p % 100:02d}"
estimate = sum(round(p, -2) for _, _, p in year) # each to the nearest rupee
nines = sum(p % 9 for _, _, p in year) % 9 == total % 9
print(f"{len(year)} payments, total {rupees(total)}; estimate {rupees(estimate)}; nines check {nines}")
for m in (1, 6, 12):
print(f" month {m:2}: {rupees(by_month[m])}")
f = 0.0
for _, _, p in year:
f += p / 100 # the same amounts as rupee floats
print("float total:", repr(f), " exact:", rupees(total), " difference in rupees:", f - total / 100)
print("float total == exact?", f == total / 100, " rounded to paise:", round(f, 2) == total / 100)
1031 payments, total ₹86,085.90; estimate ₹86,096.00; nines check True
month 1: ₹6,342.70
month 6: ₹8,107.30
month 12: ₹8,296.30
float total: 86085.8999999999 exact: ₹86,085.90 difference in rupees: -8.731149137020111e-11
float total == exact? False rounded to paise: True
Step 3: read the output. The months partition the year (the assertion passed). The estimate is within ₹11 of the exact total over a thousand payments: far closer than the worst case of ₹515.50 (1,031 payments × 50 paise), because rounding errors in both directions mostly cancel. Casting out nines agrees. The floating-point total, adding the same amounts as rupees, comes out as 86085.8999999999: not equal to the exact ₹86,085.90, and rounding to paise hides the difference. That's the worst kind of error, because every check that rounds first passes while equality comparisons in code fail. With amounts in 25-paisa steps (try it: change the 10 to 25), the float total comes out exact, because a quarter is a binary fraction; most real prices aren't.
Step 4: break it on purpose. Change one amount by 9 paise, swap two digits in one monthly total, and drop one payment. Run each check on each change and record which checks catch which errors.
Senior and architect lens
- Invariants beat spot checks. "Months add up to the year" is exact and cheap and catches lost or duplicated payments; it should run every time, not when someone remembers.
- Estimates catch scale errors, not small ones. A total off by a factor of ten jumps out of an estimate; one off by ₹9 never will. Choose checks for the errors you expect.
- Representation decides correctness. A total that is right only after rounding is a latent bug: the day someone compares two such totals for equality, or sums them further, the error surfaces. Whole paise make the question disappear.
Evidence
totals.pyand its output, for your data or the generated year.- Your hand total for one month, with its three checks.
- A table from step 4: each deliberate error, and which checks caught it.
Carry forward
Stage 4, after chapter 4, uses multiplication and division: unit prices across pack sizes, splitting shared bills fairly to the paisa, and how many months a savings target takes, with remainders handled deliberately.