Is there a limit on how many values that can be added to a boost::accumulator? If a large number of entries were added, is there any point in which the accumulator would cease to work properly or is the internal algorithm robust enough to account for a set of values approaching infinity?
#include <iostream>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/accumulators/statistics/moment.hpp>
using namespace boost::accumulators;
int main()
{
// Define an accumulator set for calculating the mean and the
// 2nd moment ...
accumulator_set<double, stats<tag::mean, tag::moment<2> > > acc;
// push in some data ...
for (std::size_t i=0; i<VERY_LARGE_NUMBER; i++)
{
acc(i);
}
// Display the results ...
std::cout << "Mean: " << mean(acc) << std::endl;
std::cout << "Moment: " << moment<2>(acc) << std::endl;
return 0;
}
If your int
is a 32 bit integer, you'll get a signed integer overflow at 46341 * 46341 when calculating moment<2>
and your program therefore has undefined behavior.
To avoid that, cast i
to the type you're using in the accumulator:
acc(static_cast<double>(i));
This will now have the same limits as a normal double
. You can add as many elements as you'd like to it as long as you don't exceed the limit (std::numeric_limits<double>::max()
) for a double
in the internal moment calculations (x
2
for moment<2>
or a sum that exceeds the limit).