Hacker News new | past | comments | ask | show | jobs | submit login

If you add together many numbers, do it in ascending order, numbers with smallest absolute values first.

Comparing floats is tricky. Use either epsilon, epsilon with relative error or ULP's (all are tricky)




That costs a sort, better to use the Kahan summation formula https://en.wikipedia.org/wiki/Kahan_summation_algorithm


Look at the accuracy comment, it's faster but less accurate than sorting first.


If the numbers happen to be >=0, one could do a little better by merging in Huffman order. In big 'O' the time complexity is the same.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: