blog-2025-11-06_float_sums

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

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)).

source


Thursday, November 6, 2025, 11:15:29 AM Coordinated Universal Time by stefs

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) nor distributive (a*(b+c) != (a*b) + (a*c)).

source


Thursday, November 6, 2025, 11:11:25 AM Coordinated Universal Time by stefs

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, commutative nor distributive.

source


Thursday, November 6, 2025, 11:07:27 AM Coordinated Universal Time by stefs

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()))

source


view