Hacker News new | ask | show | jobs
by nestorD 2645 days ago
As commented by others, this is the kind of functions for which you want to use a numerically stable algorithm.

For that you can :

- sort the numbers (clearly not the most cost effective solution but easy),

- use a high precision accumulator (which can give you an exact result if you go far enough),

- use a compensated summation (such as Kahan summation but the state of the art can do much better)

For extreme cases there are algorithms that can give you an exact result, my current favorite being Accurate Floating-Point Summation Part I: Faithful Rounding : https://www.researchgate.net/publication/228664006_Accurate_...