statsum_t.h Source File

API: statsum_t.h Source File
API
statsum_t.h
1//==========================================================================
2// STATSUM_T.H - part of
3// OMNeT++/OMNEST
4// Discrete System Simulation in C++
5//
6//==========================================================================
7
8/*--------------------------------------------------------------*
9 Copyright (C) 1992-2017 Andras Varga
10 Copyright (C) 2006-2017 OpenSim Ltd.
11
12 This file is distributed WITHOUT ANY WARRANTY. See the file
13 `license' for details on this and other legal matters.
14*--------------------------------------------------------------*/
15
16#ifndef __OMNETPP_STATSUM_T_H
17#define __OMNETPP_STATSUM_T_H
18
19#include <cmath>
20#include "simkerneldefs.h"
21#include "platdep/config.h"
22
23namespace omnetpp {
24
25namespace internals {
26
40class SIM_API NeumaierSum
41{
42 private:
43 double sum = 0;
44 double comp = 0; // running compensation for lost low-order bits
45
46 public:
48 NeumaierSum() = default;
49
51 NeumaierSum(double val) : sum(val), comp(0) {}
52
54 NeumaierSum& operator=(double val) { sum = val; comp = 0; return *this; }
55
57 operator double() const { return sum + comp; }
58
60 NeumaierSum& operator+=(double value) {
61 double t = sum + value;
62 if (std::abs(sum) >= std::abs(value))
63 comp += (sum - t) + value;
64 else
65 comp += (value - t) + sum;
66 sum = t;
67 return *this;
68 }
69};
70
71} // namespace internals
72
73using statsum_t = internals::NeumaierSum;
74
75// Legacy mode:
76// using statsum_t = double;
77
78} // namespace omnetpp
79
80#endif
NeumaierSum & operator+=(double value)
Definition statsum_t.h:60
NeumaierSum & operator=(double val)
Definition statsum_t.h:54
NeumaierSum(double val)
Definition statsum_t.h:51