YAP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CompensatedSum.h
Go to the documentation of this file.
1 /* YAP - Yet another PWA toolkit
2  Copyright 2015, Technische Universitaet Muenchen,
3  Authors: Daniel Greenwald, Johannes Rauch
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
20 
21 #ifndef yap_CompensatedSum_h
22 #define yap_CompensatedSum_h
23 
24 #include <type_traits>
25 
26 namespace yap {
27 
31 template <typename T>
34  T sum;
35 
38 
40  CompensatedSum(T val = 0) : sum(val), correction(0) {}
41 
44  operator T() const
45  { return sum; }
46 
48  CompensatedSum& operator+=(typename std::conditional<std::is_fundamental<T>::value, const T, const T&>::type value)
49  {
50  T y = value - correction;
51  T t = sum + y;
52  correction = (t - sum) - y;
53  sum = t;
54  return *this;
55  }
56 };
57 
58 }
59 
60 #endif
Struct to calculate a compensated sum using the Kahan summation algorithm.
Definition: CompensatedSum.h:32
CompensatedSum(T val=0)
constructor
Definition: CompensatedSum.h:40
CompensatedSum & operator+=(typename std::conditional< std::is_fundamental< T >::value, const T, const T & >::type value)
addition assignment operator
Definition: CompensatedSum.h:48
T correction
correction to sum
Definition: CompensatedSum.h:37
T sum
sum
Definition: CompensatedSum.h:34