Apex › Storage & databases

Kahan summation — adding floats without losing accuracy

A float has ~15–16 significant digits, so adding a small number to a large running total drops the small one's low bits — sum a million values and the lost bits become a real error. Kahan summation carries a compensation term that remembers what each addition dropped and folds it back, keeping the error near one rounding unit no matter how many terms. Watch the naive total drift while Kahan stays pinned to the true sum.

More in Storage & databases