SIMLIB/C++  3.07
stat.cc
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 //! \file stat.cc Statistics
3 //
4 // Copyright (c) 1991-2004 Petr Peringer
5 //
6 // This library is licensed under GNU Library GPL. See the file COPYING.
7 //
8 
9 //
10 // class Stat implementation
11 //
12 
13 ////////////////////////////////////////////////////////////////////////////
14 // interface
15 //
16 
17 #include "simlib.h"
18 #include "internal.h"
19 
20 #include <cmath> // sqrt()
21 
22 
23 ////////////////////////////////////////////////////////////////////////////
24 // implementation
25 //
26 
27 namespace simlib3 {
28 
30 
31 ////////////////////////////////////////////////////////////////////////////
32 // operator () --- record value
33 //
34 void Stat::operator () (double x)
35 {
36  sx += x;
37  sx2 += x*x;
38  if(++n==1) min=max=x;
39  else {
40  if(x<min) min = x;
41  if(x>max) max = x;
42  }
43 };
44 
45 
46 ////////////////////////////////////////////////////////////////////////////
47 // constructors
48 //
49 Stat::Stat(const char *name) :
50  sx(0), sx2(0),
51  min(0), max(0),
52  n(0)
53 {
54  Dprintf(("Stat::Stat(\"%s\")",name));
55  SetName(name);
56 }
57 
59  sx(0), sx2(0),
60  min(0), max(0),
61  n(0)
62 {
63  Dprintf(("Stat::Stat()"));
64 }
65 
66 ////////////////////////////////////////////////////////////////////////////
67 // destructor
68 //
70 {
71  Dprintf(("Stat::~Stat() // \"%s\" ", Name().c_str()));
72 }
73 
74 ////////////////////////////////////////////////////////////////////////////
75 // Stat::Clear --- initialize
76 //
78 {
79  sx = sx2 = 0; // sums
80  min = max = 0;
81  n = 0; // # of records
82 }
83 
84 
85 ////////////////////////////////////////////////////////////////////////////
86 // Stat::MeanValue
87 //
88 double Stat::MeanValue() const
89 {
90  if (n==0) SIMLIB_error(StatNoRecError);
91  return sx/n;
92 }
93 
94 ////////////////////////////////////////////////////////////////////////////
95 // Stat::StdDev --- standard deviation
96 //
97 double Stat::StdDev() const
98 {
100  double mv = sx/n;
101  return sqrt((sx2-n*mv*mv)/(n-1));
102 }
103 
104 }
105 // end
void SIMLIB_error(const enum _ErrEnum N)
print error message and abort program
Definition: error.cc:38
virtual void Clear()
initialize
Definition: stat.cc:77
void SetName(const std::string &name)
assign the name
Definition: object.cc:125
double max
Definition: simlib.h:568
double sx2
Definition: simlib.h:566
Implementation of class CalendarList interface is static - using global functions in SQS namespace...
Definition: algloop.cc:32
void operator()(double x)
record the value
Definition: stat.cc:34
double StdDev() const
Definition: stat.cc:97
Internal header file for SIMLIB/C++.
double sx
Definition: simlib.h:565
Main SIMLIB/C++ interface.
unsigned long n
Definition: simlib.h:569
virtual std::string Name() const
get object name
Definition: object.cc:134
double MeanValue() const
Definition: stat.cc:88
SIMLIB_IMPLEMENTATION
Definition: algloop.cc:34
#define Dprintf(f)
Definition: internal.h:100
double min
Definition: simlib.h:567