![]() System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.33 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, Directory : /usr/include/boost/histogram/accumulators/ |
Upload File : |
// Copyright 2018 Hans Dembinski // // Distributed under the Boost Software License, version 1.0. // (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_HISTOGRAM_ACCUMULATORS_SUM_HPP #define BOOST_HISTOGRAM_ACCUMULATORS_SUM_HPP #include <boost/histogram/fwd.hpp> #include <cmath> #include <type_traits> namespace boost { namespace histogram { namespace accumulators { /** Uses Neumaier algorithm to compute accurate sums. 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. */ template <typename RealType> class sum { public: sum() = default; /// Initialize sum to value explicit sum(const RealType& value) noexcept : large_(value) {} /// Set sum to value sum& operator=(const RealType& value) noexcept { large_ = value; small_ = 0; return *this; } /// Increment sum by one sum& operator++() { return operator+=(1); } /// Increment sum by value sum& operator+=(const RealType& value) { auto temp = large_ + value; // prevent optimization if (std::abs(large_) >= std::abs(value)) small_ += (large_ - temp) + value; else small_ += (value - temp) + large_; large_ = temp; return *this; } /// Scale by value sum& operator*=(const RealType& value) { large_ *= value; small_ *= value; return *this; } template <class T> bool operator==(const sum<T>& rhs) const noexcept { return large_ == rhs.large_ && small_ == rhs.small_; } template <class T> bool operator!=(const T& rhs) const noexcept { return !operator==(rhs); } /// Return large part of the sum. const RealType& large() const { return large_; } /// Return small part of the sum. const RealType& small() const { return small_; } // allow implicit conversion to RealType operator RealType() const { return large_ + small_; } template <class Archive> void serialize(Archive&, unsigned /* version */); private: RealType large_ = RealType(); RealType small_ = RealType(); }; } // namespace accumulators } // namespace histogram } // namespace boost #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED namespace std { template <class T, class U> struct common_type<boost::histogram::accumulators::sum<T>, boost::histogram::accumulators::sum<U>> { using type = boost::histogram::accumulators::sum<common_type_t<T, U>>; }; } // namespace std #endif #endif