SIMLIB/C++ 3.09
Loading...
Searching...
No Matches
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
27namespace simlib3 {
28
30
31////////////////////////////////////////////////////////////////////////////
32// operator () --- record value
33//
34void 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//
49Stat::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//
88double Stat::MeanValue() const
89{
91 return sx/n;
92}
93
94////////////////////////////////////////////////////////////////////////////
95// Stat::StdDev --- standard deviation
96//
97double Stat::StdDev() const
98{
100 double mv = sx/n;
101 return sqrt((sx2-n*mv*mv)/(n-1));
102}
103
104}
105// end
void SetName(const std::string &name)
assign the name
Definition: object.cc:125
virtual std::string Name() const
get object name
Definition: object.cc:134
double StdDev() const
Definition: stat.cc:97
virtual void Clear()
initialize
Definition: stat.cc:77
double min
Definition: simlib.h:567
void operator()(double x)
record the value
Definition: stat.cc:34
double max
Definition: simlib.h:568
double MeanValue() const
Definition: stat.cc:88
unsigned long n
Definition: simlib.h:569
double sx
Definition: simlib.h:565
double sx2
Definition: simlib.h:566
@ StatNoRecError
Definition: errors.h:71
@ StatDispError
Definition: errors.h:72
Internal header file for SIMLIB/C++.
#define Dprintf(f)
Definition: internal.h:100
Implementation of class CalendarList interface is static - using global functions in SQS namespace.
Definition: algloop.cc:32
void SIMLIB_error(const enum _ErrEnum N)
print error message and abort program
Definition: error.cc:38
SIMLIB_IMPLEMENTATION
Definition: algloop.cc:34
double min(double a, double b)
Definition: internal.h:285
double max(double a, double b)
Definition: internal.h:286
Main SIMLIB/C++ interface.