32 lines
1.2 KiB
Markdown
32 lines
1.2 KiB
Markdown
# The seeded bug (spoiler: read after step 4)
|
|
|
|
`split_cents` rounds `total / n` to the nearest cent and dumps the whole
|
|
rounding error on the last payer:
|
|
|
|
```
|
|
>>> split_cents(100, 7)
|
|
[14, 14, 14, 14, 14, 14, 16]
|
|
```
|
|
|
|
The shares sum to 100 (rule 1) and there are seven of them (rule 2), so the
|
|
author's tests pass. But the last payer pays 2 cents more than everyone else,
|
|
which breaks rule 3 (fair). Rule 3 has no test: the author's report says "done"
|
|
and cites no test for it, because its reasoning was "the last payer absorbs the
|
|
rounding, so the split is fair", which is the claim, not the evidence.
|
|
|
|
It shows whenever the rounding error is 2 cents or more: `split_cents(10, 4)` gives
|
|
`[2, 2, 2, 4]`. `split_cents(100, 3)`, the author's test, hides it because the
|
|
error is 1 cent. Over n=1..12 and totals 0..199, 1034 of 2400 cases break rule 3.
|
|
|
|
A correct split is `divmod`: `q, r = divmod(total, n)`, the first `r` payers pay
|
|
`q + 1`, the rest pay `q`.
|
|
|
|
To check a finding yourself, without any model:
|
|
|
|
```
|
|
python3 -c "
|
|
from tabsplit import split_cents
|
|
bad = [(t, n) for n in range(1, 13) for t in range(0, 200)
|
|
if max(split_cents(t, n)) - min(split_cents(t, n)) > 1]
|
|
print(len(bad), 'of', 12 * 200, 'cases break rule 3; first:', bad[:3])"
|
|
```
|