Scippy

SoPlex

Sequential object-oriented simPlex

stablesum.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the class library */
4 /* SoPlex --- the Sequential object-oriented simPlex. */
5 /* */
6 /* Copyright (C) 1996-2019 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SoPlex is distributed under the terms of the ZIB Academic Licence. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SoPlex; see the file COPYING. If not email to soplex@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 #ifndef _SOPLEX_STABLE_SUM_H_
17 #define _SOPLEX_STABLE_SUM_H_
18 
19 #include <type_traits>
20 
21 namespace soplex
22 {
23 
24 template <typename T>
25 class StableSum
26 {
27  typename std::remove_const<T>::type sum;
28 
29 public:
30  StableSum() : sum(0) {}
31  StableSum(const T& init) : sum(init) {}
32 
33  void operator+=(const T& input)
34  {
35  sum += input;
36  }
37 
38  void operator-=(const T& input)
39  {
40  sum -= input;
41  }
42 
43  operator typename std::remove_const<T>::type() const
44  {
45  return sum;
46  }
47 };
48 
49 template <>
50 class StableSum<double>
51 {
52  double sum = 0;
53  double c = 0;
54 
55 public:
56  StableSum() = default;
57  StableSum(double init) : sum(init), c(0) {}
58 
59  void operator+=(double input)
60  {
61 #if defined(_MSC_VER) || defined(__INTEL_COMPILER)
62 #pragma float_control( precise, on )
63 #endif
64 
65  double t = sum + input;
66  double z = t - sum;
67  double y = (sum - (t - z)) + (input - z);
68  c += y;
69 
70  sum = t;
71  }
72 
73  void operator-=(double input)
74  {
75  (*this) += -input;
76  }
77 
78  operator double() const
79  {
80  return sum + c;
81  }
82 };
83 }
84 
85 #endif
void operator+=(double input)
Definition: stablesum.h:59
void operator-=(double input)
Definition: stablesum.h:73
void operator+=(const T &input)
Definition: stablesum.h:33
Everything should be within this namespace.
std::remove_const< T >::type sum
Definition: stablesum.h:27
void operator-=(const T &input)
Definition: stablesum.h:38
StableSum(const T &init)
Definition: stablesum.h:31