source: trunk/src/Statistics.cc @ 115

Last change on this file since 115 was 115, checked in by Peter, 18 years ago

added

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 879 bytes
Line 
1// $Id: Statistics.cc 115 2004-07-19 14:38:43Z peter $
2
3// Thep C++ Tools
4#include "Statistics.h"
5
6// System includes
7#include <vector>
8#include <algorithm>
9
10#include <cstdlib>
11#include <iostream>
12
13
14namespace theplu {
15namespace cpptools { 
16
17  Statistics::Statistics(void) 
18     
19  {
20  }
21
22  double Statistics::median(std::vector<double>& vec) 
23  {
24    return percentile(vec, 50.0);
25  }
26
27  double Statistics::percentile(std::vector<double>& vec, double percentile) 
28  {
29    sort(vec.begin(), vec.end());
30    double j = percentile/100 * vec.size();
31    if (percentile==100)
32      return vec[vec.size()-1];   
33   
34    double k = j + 0.5 - static_cast<int> (j+0.5);
35    if (k<.500000000001 && k>.499999999999){
36      int i = static_cast<int>(j+0.5);
37      double r = (vec[i]+vec[i-1])/2;
38     
39      return r;
40      //return (vec[i]+vec[i-1])/2;
41    }
42    else{
43      int i = static_cast<int>(j);
44      return  vec[i];
45    }
46   
47  }
48
49
50}} // of namespace cpptools and namespace theplu
Note: See TracBrowser for help on using the repository browser.