DiSMEC++
sum.h
Go to the documentation of this file.
1 // Copyright (c) 2021, Aalto University, developed by Erik Schultheis
2 // All rights reserved.
3 //
4 // SPDX-License-Identifier: MIT
5 
6 #ifndef DISMEC_SRC_UTILS_SUM_H
7 #define DISMEC_SRC_UTILS_SUM_H
8 
9 namespace dismec {
10  // https://en.wikipedia.org/wiki/Kahan_summation_algorithm
17  template<class Float>
19  public:
20  KahanAccumulator() = default;
21 
22  Float value() const { return Sum; }
23 
25  if (value > Sum) {
27  Sum = value;
28  } else {
30  }
31  return *this;
32  }
33 
34  private:
35  static void accumulate(Float& accumulator, Float& correction, Float addition) {
36  Float y = addition - correction;
37  volatile Float t = accumulator + y;
38  volatile Float z = t - accumulator;
39  correction = z - y;
40  accumulator = t;
41  }
42 
43  Float Sum = Float{0};
44  Float Correction = Float{0};
45  };
46 }
47 #endif //DISMEC_SRC_UTILS_SUM_H
Implements a numerically stable sum algorithm.
Definition: sum.h:18
static void accumulate(Float &accumulator, Float &correction, Float addition)
Definition: sum.h:35
KahanAccumulator & operator+=(Float value)
Definition: sum.h:24
Float value() const
Definition: sum.h:22
Main namespace in which all types, classes, and functions are defined.
Definition: app.h:15