Quant Memo
Coding/●●●●●

Storing money in floats is a bug waiting to reconcile

A trading dashboard keeps a running cash balance as a Python float. After a day of small credits and debits it is off from the clearing statement by a few cents:

>>> bal = 0.0
>>> for _ in range(10):
...     bal += 0.1
>>> bal
0.9999999999999999

Explain why floats are the wrong type for a ledger, and write an add_cash(cents_total, dollars_str) that keeps money exact. What should the ledger store internally?

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions