The Ledger That Leaked Cents — Python Bug Hunt
Inspired by every audit that ever found the books off by a few cents — because prices took a round-trip through binary floating point.
- Language: Python
- Layer: Database
- Difficulty: Easy
- Concepts: Floating Point, Money
- Modelled on: Floating-point money lore
- Visible tests: the classic 19.99 is exact; sub-dollar prices are exact; whole and single-decimal prices pad
- Reward: 50 XP for a complete fix
Incident
- Severity: SEV-2 (priority: High)
- Ticket: BUG-1CENT
- Reported by: finance close
- Time to fix: 10 minutes, the target for an easy incident
Failing checks on the shipped build
- the classic 19.99 is exact — expected 1999 but got 1998
- sub-dollar prices are exact — expected 29 but got 28
How to work this incident
- Read the report: write down the expected behaviour and the actual one in a sentence each.
- Reproduce it: run the visible tests and find the failing check that matches the report.
- Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
- Follow the data through the query or the repository code; compare the rows it returns with the rows the report expects.
- Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.
Briefing
Inspired by every audit that ever found the books off by a few cents — because prices took a round-trip through binary floating point. 19.99 * 100 is 1998.9999…, and int() finishes the crime.
prices.py parses price strings into integer cents. No floats allowed anywhere.
Bug report
BUG-1CENT · Priority: High · Reported by: finance close
parse_cents(price) — strings with exactly 0–2 decimals:
- "19.99" -> 1999
- "0.29" -> 29
- "5" -> 500
- "7.5" -> 750
Observed: "19.99" parses to 1998. Every 19.99 sale leaks a cent.
Logs
[close] ledger drift this quarter: -$142.17The code as shipped
src/fin/prices.py (editable)
# Parses a price string into integer cents.
def parse_cents(price):
return int(float(price) * 100)
Open the hunt to start the incident clock, edit the files, run the visible tests and submit against the hidden ones, then write the root cause down. More Python bug hunts.