Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards


Class template sum

boost::histogram::accumulators::sum — Uses Neumaier algorithm to compute accurate sums of floats.


// In header: <boost/histogram/accumulators/sum.hpp>

template<typename ValueType> 
class sum {
  // types
  typedef ValueType          value_type;     
  typedef const value_type & const_reference;

  // construct/copy/destruct
  sum() = default;
  sum(const_reference) noexcept;
  template<typename T> sum(const sum< T > &) noexcept;
  sum(const_reference, const_reference) noexcept;

  // public member functions
  sum & operator++() noexcept;
  sum & operator+=(const_reference) noexcept;
  sum & operator+=(const sum &) noexcept;
  sum & operator*=(const_reference) noexcept;
  bool operator==(const sum &) const noexcept;
  bool operator!=(const sum &) const noexcept;
  value_type value() const noexcept;
  const_reference large_part() const noexcept;
  const_reference small_part() const noexcept;
  explicit operator value_type() const noexcept;
  template<typename Archive> void serialize(Archive &, unsigned);
  sum & operator*=(const sum &) noexcept;
  sum operator*(const sum &) const noexcept;
  sum & operator/=(const sum &) noexcept;
  sum operator/(const sum &) const noexcept;
  bool operator<(const sum &) const noexcept;
  bool operator>(const sum &) const noexcept;
  bool operator<=(const sum &) const noexcept;
  bool operator>=(const sum &) const noexcept;


The algorithm is an improved Kahan algorithm ( The algorithm uses memory for two numbers and is three to five times slower compared to using a single number 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/destruct

  1. sum() = default;
  2. sum(const_reference value) noexcept;
    Initialize sum to value and allow implicit conversion.
  3. template<typename T> sum(const sum< T > & s) noexcept;
    Allow implicit conversion from sum<T>
  4. sum(const_reference large_part, const_reference small_part) noexcept;
    Initialize sum explicitly with large and small parts.

sum public member functions

  1. sum & operator++() noexcept;
    Increment sum by one.
  2. sum & operator+=(const_reference value) noexcept;
    Increment sum by value.
  3. sum & operator+=(const sum & s) noexcept;
    Add another sum.
  4. sum & operator*=(const_reference value) noexcept;
    Scale by value.
  5. bool operator==(const sum & rhs) const noexcept;
  6. bool operator!=(const sum & rhs) const noexcept;
  7. value_type value() const noexcept;
    Return value of the sum.
  8. const_reference large_part() const noexcept;
    Return large part of the sum.
  9. const_reference small_part() const noexcept;
    Return small part of the sum.
  10. explicit operator value_type() const noexcept;
  11. template<typename Archive> void serialize(Archive & ar, unsigned);
  12. sum & operator*=(const sum & rhs) noexcept;
  13. sum operator*(const sum & rhs) const noexcept;
  14. sum & operator/=(const sum & rhs) noexcept;
  15. sum operator/(const sum & rhs) const noexcept;
  16. bool operator<(const sum & rhs) const noexcept;
  17. bool operator>(const sum & rhs) const noexcept;
  18. bool operator<=(const sum & rhs) const noexcept;
  19. bool operator>=(const sum & rhs) const noexcept;