preCICE v3.1.2
Loading...
Searching...
No Matches
DoubleAggregator.hpp
Go to the documentation of this file.
1#include <boost/accumulators/accumulators.hpp>
2#include <boost/accumulators/statistics.hpp>
3#include <boost/accumulators/statistics/sum_kahan.hpp>
4
5namespace precice::utils {
6
9private:
11 using Acc = boost::accumulators::accumulator_set<double, boost::accumulators::stats<boost::accumulators::tag::sum_kahan>>;
12
13public:
15 void operator=(double d)
16 {
17 acc = Acc{};
18 acc(d);
19 }
20
22 void add(double d)
23 {
24 acc(d);
25 }
26
28 void operator+=(double d)
29 {
30 add(d);
31 }
32
34 void operator-=(double d)
35 {
36 add(-d);
37 }
38
40 double value() const
41 {
42 return boost::accumulators::sum_kahan(acc);
43 }
44
46 operator double() const
47 {
48 return value();
49 }
50
51private:
53};
54
55[[nodiscard]] inline DoubleAggregator operator+(double lhs, DoubleAggregator rhs)
56{
57 rhs.add(lhs);
58 return rhs;
59}
60
61[[nodiscard]] inline DoubleAggregator operator+(DoubleAggregator lhs, double rhs)
62{
63 lhs.add(rhs);
64 return lhs;
65}
66
67[[nodiscard]] inline DoubleAggregator operator-(DoubleAggregator lhs, double rhs)
68{
69 lhs.add(-rhs);
70 return lhs;
71}
72
73} // namespace precice::utils
An accurate aggregator for doubles with usability in mind.
boost::accumulators::accumulator_set< double, boost::accumulators::stats< boost::accumulators::tag::sum_kahan > > Acc
This class is mainly a wrapper for boost's Kahan accumulator which implements the Kahan summation alg...
void operator-=(double d)
Natural version of subtracting a value.
void operator+=(double d)
Natural version of adding a value.
double value() const
Retrieves the corrected sum.
void add(double d)
Adds a value to the aggregator.
void operator=(double d)
Sets the aggregator to a value.
contains precice-related utilities.
DoubleAggregator operator+(double lhs, DoubleAggregator rhs)
DoubleAggregator operator-(DoubleAggregator lhs, double rhs)