# Accurate summation algorithm

**URL:** https://discourse.julialang.org/t/accurate-summation-algorithm/7163
**Category:** Numerics
**Created:** [November 19, 2017, 1:51am UTC](https://discourse.julialang.org/t/accurate-summation-algorithm/7163 "2017-11-19T01:51:17Z")
**Posts on this page:** 1
**Showing post:** 9

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [November 22, 2017, 10:44am UTC](https://discourse.julialang.org/t/accurate-summation-algorithm/7163/9 "2017-11-22T10:44:30Z")

</div>

A point I’ve made several times previously is the following:

- Mathematically, since + is associative and commutative, all correct summation algorithms are algebraically equivalent to each other, in particular they are all equivalent to left-to-right summation.

- The `-ffast-math` option gives the compiler license to transform any algorithm into any algebraically equivalent one in the mathematical sense.

- Therefore `-ffast-math` gives the compiler license to transform any summation algorithm into any other summation algorithm, including left-to-right summation.

That’s why there’s no fundamentally useful answer to this question: the only thing you can find out is how a _particular_ compiler happens to optimize your code. A different compiler in `-ffast-math` mode or a different version of your own compiler – or _the exact same compiler_ targeting a different architecture – can always make the situation different and potentially much worse. The example above happens to outsmart one particular compiler, but as @mbauman’s post shows, a slightly smarter compiler could straightforwardly overcome that obstacle and make this exact same code fail. Or put another way, there are no reliable summation algorithms in `-ffast-math` mode.

The same argument applies to superaccumulators in principle, but they typically operate on floating-point values reinterpreted as integers which `-ffast-math` gives no additional license to optimize, so they should be safe enough. They’re _much_ slower than approximate summation algorithms, however.

---

_[View the full topic](https://discourse.julialang.org/t/accurate-summation-algorithm/7163)._
