Computing average without overflows and with half-decent precision At some point I needed to compute the average of a big collection of integers. I knew the size of the collection prior computation, but a naive average computation was prone to integer overflow while repeatedly adding every elements divided by the size was prone to loss of precision. To mitigate overflow and loss of precision (speed wasn't an issue), I came up with the following algorithm:

#include &lt;cstdint&gt;
#include &lt;iterator&gt;
#include &lt;type_traits&gt;

template&lt;typename RandomAccessIterator&gt;
auto average(RandomAccessIterator first, RandomAccessIterator last)
    -&gt; long double
{
    static_assert(std::is_integral&lt;
        typename std::iterator_traits&lt;RandomAccessIterator&gt;::value_type
    &gt;::value, "average only available for built-in integer types");

    auto size = std::distance(first, last);
    if (size == 0) return 0.0L;

    std::intmax_t accumulator = 0;
    long double res = 0.0L;

    while (first != last) {
        std::intmax_t tmp;
        if (__builtin_add_overflow(*first, accumulator, &amp;tmp)) {
            res += static_cast&lt;long double&gt;(accumulator) / static_cast&lt;long double&gt;(size);
            accumulator = *first;
        } else {
            accumulator = tmp;
        }
        ++first;
    }

    res += static_cast&lt;long double&gt;(accumulator) / static_cast&lt;long double&gt;(size);
    return res;
}


Basically, here is what the program does:


It initializes the result and an accumulator with 0.
It accumulates the values from the collection in an std::intmax_t accumulator as long as said accumulator does not overflow.
When the accumulator is on the verge of overflowing, the accumulator is divided by the size of the collection and added to the result. It then takes the value that would have made it overflow. The loop continues.
When then is nothing more to treat in the collection, the current value of the accumulator is divided by the size of the collection and added to the result.


I think that the goal of avoiding overflows is reached: the accumulation is guarded against overflow, and the division of every element to add by the size ensures that the result itself can't overflow either.

Regarding the precision, adding integers in an std::intmax_t as long as possible ensures that no precision is lost before the division. In the best case, a single division will occur. Moreover, the algorithm should divide bit integers most of the time, which may help the precision too. I picked long double to have the best precision guaranteed by the standard too.

Now, there are a few known shortcomings, to name a few:


Returning 0.0L is probably not the best solution when the collection is empty, but I'm not sure that returning NaN would be better either. One way to circumvent that would be not to handle the case, and document it as a precondition.
__builtin_add_overflow is a GCC builtin, not a standard C++ function. Unfortunately, I don't know any standard equivalent to it, and manually checking for overflows is hard to do correctly.


The algorithm obviously uses O(1) extra memory. Now, did I properly manage to correctly avoid overflow while mitigating precision loss? Is there anything else to be said about this code?
First, having a forward-iterator is good enough, don't ask for more. Yes, it might mean you have to go over the range twice (once to measure its size), but that's a minor irritant.
Just please, verify it is a forward-iterator like this:

std::forward_iterator_tag = std::iterator_traits&lt;ForwardIt&gt;::iterator_category{};

Use the right loop for the job. It's more readable, and more concise.
Mark any variable you can const to help comprehension and error-checking.
Next, you really have to improve your overflow-handling, as res += static_cast&lt;long double&gt;(accumulator) / static_cast&lt;long double&gt;(size); might loose a small amount of precision.
Stay in the integer-realm by adding / subtracting enough multiples of size to change the sign of accumulator.
Don't cast to long double unless you can guarantee perfect accuracy.
You can easily supplant the builtin with a bit of logic:
Test the sign of accumulate and depending on the result check whether MAXVALUE - accumulate &gt; *first or MINVALUE - accumulate &lt; *first.
Not sure whether the compiler can optimize that as well as the builtin though...
Consider whether making it SFINAE-friendly by avoiding hard errors might be worth the more verbose error-messages.


Modified code:

#include &lt;cstdint&gt;
#include &lt;iterator&gt;
#include &lt;type_traits&gt;
#include &lt;limits&gt;

template&lt;typename ForwardIt&gt;
long double average(ForwardIt first, ForwardIt last)
{
    static_assert(std::is_integral&lt;typename std::iterator_traits&lt;ForwardIt&gt;::value_type&gt;(),
        "average only available for built-in integer types");
    std::forward_iterator_tag = std::iterator_traits&lt;ForwardIt&gt;::iterator_category();

    const auto size = std::distance(first, last);
    if (!size) return 0;

    std::intmax_t accumulator = 0;
    long double res = 0;
    do {
        using snl = std::numeric_limits&lt;decltype(accumulator)&gt;;
        if (accumulator &lt; 0 &amp;&amp; snl::min() - accumulator &gt; *first) {
            res += accumulator / size - 1;
            accumulator = accumulator % size + size;
        } else if (accumulator &gt; 0 &amp;&amp; snl::max() - accumulator &lt; *first) {
            res += accumulator / size + 1;
            accumulator = accumulator % size - size;
        }
        accumulator += *first;
    } while (++first != last);
    res += accumulator / size;
    res += static_cast&lt;decltype(res)&gt;(accumulator % size) / size;
    return res;
}