{ "name": "KahanSum", "summary": "The sum of a list of FLOAT values by compensated summation, whose error bound, to first order, does not grow with the length of the list.", "tags": [ "summation", "rounding", "precision" ], "sources": [ { "title": "Kahan, Pracniques: further remarks on reducing truncation errors, CACM 8(1), 1965", "url": "https://doi.org/10.1145/363707.363723" }, { "title": "Higham, Accuracy and Stability of Numerical Algorithms, 2nd edition (SIAM, 2002), section 4.3, Compensated summation" } ], "cases": [ { "call": "KahanSum([1.0, 2.0, 3.0])" }, { "call": "KahanSum([1.0, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16])" }, { "call": "KahanSum([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1])" }, { "call": "KahanSum([])" } ], "references": [ { "lang": "python", "level": "plain", "file": "ref/python/plain.py", "note": "The same compensated loop." }, { "lang": "c", "level": "plain", "file": "ref/c/plain.c", "note": "The same loop in C. It survives -O2; it would not survive -ffast-math, which lets the compiler treat (t - total) - y as 0." }, { "lang": "mojo", "level": "plain", "file": "ref/mojo/plain.mojo", "note": "The same loop in Mojo." } ], "proof": { "lang": "dafny", "file": "proof/kahan_sum.dfy", "note": "Over real numbers the compensation C stays 0 and the result is exactly the sum: what Kahan summation corrects is rounding, and nothing else." } }