Get the latest tech news

Taming floating-point sums


ppose you have an array of floating-point numbers, and wish to sum them. You might naively think you can simply add them, e.g.

This can actually compute a completely exact sum, without any rounding at all, and is effectively just an overly permissive representation of a fixed-point number optimized for accumulating floats. Note that the real numbers do not contain NaNs or infinities, so these operators assume those do not exist for the validity of transformations, however it is not undefined behavior when you do encounter those values. Now this might not be entirely fair as the slowest methods are computing something significantly harder, but there’s still a 20x performance difference between a seemingly reasonable naive implementation and the fastest one.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Point Sums

Point Sums