...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::histogram::accumulators::sum — Uses Neumaier algorithm to compute accurate sums.
// In header: <boost/histogram/accumulators/sum.hpp> template<typename RealType> class sum { public: // construct/copy/destruct sum() = default; explicit sum(const RealType &) noexcept; sum & operator=(const RealType &) noexcept; // public member functions sum & operator++(); sum & operator+=(const RealType &); sum & operator *=(const RealType &); template<typename T> bool operator==(const sum< T > &) const noexcept; template<typename T> bool operator!=(const T &) const noexcept; const RealType & large() const; const RealType & small() const; operator RealType() const; template<typename Archive> void serialize(Archive &, unsigned); };
The algorithm uses memory for two floats and is three to five times slower compared to a simple floating point number used to accumulate a sum, but the relative error of the sum is at the level of the machine precision, independent of the number of samples.
A. Neumaier, Zeitschrift fuer Angewandte Mathematik und Mechanik 54 (1974) 39-51.
sum
public
construct/copy/destructsum() = default;
explicit sum(const RealType & value) noexcept;Initialize sum to value.
sum & operator=(const RealType & value) noexcept;Set sum to value.
sum
public member functionssum & operator++();Increment sum by one.
sum & operator+=(const RealType & value);Increment sum by value.
sum & operator *=(const RealType & value);Scale by value.
template<typename T> bool operator==(const sum< T > & rhs) const noexcept;
template<typename T> bool operator!=(const T & rhs) const noexcept;
const RealType & large() const;Return large part of the sum.
const RealType & small() const;Return small part of the sum.
operator RealType() const;
template<typename Archive> void serialize(Archive & ar, unsigned);