
A simulation aggregates floating-point corrections after adding and subtracting a large baseline. A small legitimate correction can disappear when intermediate sums lose precision. The large cancellation case exposes a contribution a naïve accumulator can lose. Accurate summation improves the addition step for finite inputs.
Run the calculation locally
The script runs math.fsum against three independent inputs. Each call starts from the value shown, so changing one example leaves the other expectations available for comparison. The table records real return values from the example. Quotation marks around text in the table make its boundaries visible.
import math
def evaluate(value):
return math.fsum(value)
for sample in [[1e+16, 1, -1e+16], [0.1, 0.1, 0.1], [3, -3]]:
print(evaluate(sample))Inspect the boundary cases
| Input to evaluate | Observed result |
|---|---|
| [1e+16, 1, -1e+16] | 1.0 |
| [0.1, 0.1, 0.1] | 0.3 |
| [3, -3] | 0.0 |
For [1e+16, 1, -1e+16], the observed output is 1.0. With [0.1, 0.1, 0.1], it becomes 0.3. If a second implementation produces a different answer, begin with these small inputs. They allow a reviewer to trace the expression without reconstructing the entire application or replaying a production workload.
The value for [3, -3] is 0.0. Record the unit or indexing convention next to that expectation. A second implementation can return the same type and still disagree about where a boundary falls or which observations enter the calculation.
Check the calling code
A more accurate sum does not repair errors introduced while producing the individual measurements, and currency rules still need explicit decimal handling.
A practical test can begin with assert evaluate([1e+16, 1, -1e+16]) == 1.0. Add the other rows as separate expectations so one failure identifies the input that changed. For a service fed by stored data, repeat the check on one anonymized record from an earlier version of its file or message format.
One more fixture before integrating
Feed the same values in a different order to the complete calculation. A result that remains stable under a small fixture can still differ in a longer aggregation pipeline when intermediate stages round or convert units.
The same Python function was also run with [] / [1e+16] / [-1e+16, 1, 1e+16]. These additional inputs change the boundary without changing the implementation. An exception name in the table records where the operation rejects an input; a returned empty or zero value records a successful call. Choose deliberately which of those outcomes the surrounding feature should expose.
| Additional fixture | Result or exception |
|---|---|
| [] | 0.0 |
| [1e+16] | 1e+16 |
| [-1e+16, 1, 1e+16] | 1.0 |
Python’s math reference · Python standard library
Sources and further reading
Cover photograph: 2018 Bay Area Mechanical Keyboard Meetup (45034409185) by Patrick Breen. CC BY 2.0. Cropped and typeset for this cover. The photograph illustrates the topic.
