ngscopeclient v0.1-rc1
KahanSummation.h
1/***********************************************************************************************************************
2* *
3* libscopehal *
4* *
5* Copyright (c) 2012-2025 Andrew D. Zonenberg and contributors *
6* All rights reserved. *
7* *
8* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
9* following conditions are met: *
10* *
11* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
12* following disclaimer. *
13* *
14* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
15* following disclaimer in the documentation and/or other materials provided with the distribution. *
16* *
17* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
18* derived from this software without specific prior written permission. *
19* *
20* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
21* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
22* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
26* POSSIBILITY OF SUCH DAMAGE. *
27* *
28***********************************************************************************************************************/
29
30#ifndef KahanSummation_h
31#define KahanSummation_h
32
37{
38public:
40 { Reset(); }
41
43 void Reset(float f = 0)
44 {
45 m_partialSum = f;
46 m_errorTerm = 0;
47 }
48
50 float operator+=(float rhs)
51 {
52 float y = rhs - m_errorTerm;
53 float t = m_partialSum + y;
54 m_errorTerm = (t - m_partialSum) - y;
55 m_partialSum = t;
56 return m_partialSum;
57 }
58
59 float GetSum()
60 { return m_partialSum; }
61
62protected:
63
64 //The working sum
65 float m_partialSum;
66
67 //Error term (low bits that got rounded off)
68 float m_errorTerm;
69};
70
71#endif
Helper class for performing a Kahan summation on a large number of floating point numbers.
Definition: KahanSummation.h:37
float operator+=(float rhs)
Add a new value to the sum.
Definition: KahanSummation.h:50
void Reset(float f=0)
Resets the sum (default to zero)
Definition: KahanSummation.h:43