blog-2025-11-06_float_sums

Suppose you have an array of floating-point numbers, and wish to sum them. You might naively think you can simply add them This however can easily result in an arbitrarily large accumulated error. Taming Floating-Point Sums

tried this in kotlin, where even the built in sum function uses the naive approach:

// prints 16777216,00
println("%.2f".format(FloatArray(100_000_000) { 1.0f }.sum()))

floating point addition isn't associative (a+(b+c) != (a+b)+c), not strictly commutative (a+b == b+a, but NaN != NaN, but there are even more special cases) nor distributive (a*(b+c) != (a*b) + (a*c)).


nothing's linking here.

last edited by: stefs at Thursday, November 6, 2025, 11:16:47 AM Coordinated Universal Time


edit history source