-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkahan.hpp
64 lines (55 loc) · 986 Bytes
/
kahan.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef _KAHAN_HPP_
#define _KAHAN_HPP_
template<typename T>
struct kahan_sum
{
T s,c,y,t;
kahan_sum() : s(0.),c(0.),y(0.),t(0.){}
kahan_sum<T> & operator=(const kahan_sum<T> & rhs)
{
this->s = rhs.s;
this->c = rhs.c;
this->y = rhs.y;
this->t = rhs.t;
return *this;
}
T & operator()( const T & v,const T & i )
//! For use in algorithms (std::accumulate)
{
y=i-c;
t=s+y;
c=(t-s)-y;
s=t;
return s;
}
kahan_sum<T> & operator+=(const T & i)
//!For accurate summation in blocks of code
{
y=i-c;
t=s+y;
c=(t-s)-y;
s=t;
return *this;
}
kahan_sum<T> & operator/=(const T & i)
{
s /= i;
return *this;
}
kahan_sum<T> & operator=(const T & i)
{
s = i;
return *this;
}
kahan_sum<T> operator/(const T & i) const
{
kahan_sum<T> temp;
temp.s = this->s/i;
return temp;
}
operator double() const
{
return s;
}
};
#endif /* _KAHAN_HPP_ */