Quant Memo
Coding/●●●●●

Why summing 0.1 ten times misses 1.0, and how to fix it

A risk report totals thousands of small position values. A junior notices a sanity check fails:

>>> sum([0.1] * 10)
0.9999999999999999
>>> sum([0.1] * 10) == 1.0
False

Explain why a plain running sum drifts, and write a compensated_sum(values) that recovers almost all of the lost precision. What is its cost?

Your answer

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

More Coding questions