I am aware of a similar question, but I want to ask for people opinion on my algorithm to sum floating point numbers as accurately as possible with practical costs.
Kahan's summation algorithm is significantly more precise than straightforward summation, and it runs in O(n) (somewhere between 1-4 times slower than straightforward summation depending how fast floating-point is compared to data access. Definitely less than 4 times slower on desktop hardware, and without any shuffling around of data).
Alternately, if you are using the usual x86 hardware, and if your compiler allows access to the 80-bit long double
type, simply use the straightforward summation algorithm with the accumulator of type long double
. Only convert the result to double
at the very end.
If you really need a lot of precision, you can combine the above two solutions by using long double
for variables c
, y
, t
, sum
in Kahan's summation algorithm.