Boost logo

Boost :

Subject: [boost] Interest in algorithm for arithmetic mean of integers
From: Matt Calabrese (rivorus_at_[hidden])
Date: 2015-09-03 19:36:41


AFAICT the only function for calculating arithmetic mean in boost is in the
accumulators library and it's not really appropriate for integral types. In
practice, properly calculating the mean for a range (especially a range
that's only an input range but not a forward range) is surprisingly tricky
to do in a way that avoids overflow and provides an exact result. I have a
couple of solutions, including one that works for input ranges.

Is this something that people would find useful? Is there a similar
algorithm already implemented in boost that I'm not aware of? The approach
I've taken yields an exact result as a mixed number and should work without
overflow (assuming only that the range's size is able to be represented by
its difference type). I believe the implementation that works for input
ranges could theoretically be adapted to work with Boost.Accumulators,
though I'm admittedly not too familiar with the library.

-- 
-Matt Calabrese

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk