Changeset 620


Ignore:
Timestamp:
Sep 4, 2006, 5:16:57 PM (15 years ago)
Author:
Markus Ringnér
Message:

Fixed problems with Averager that sometimes appeared as failures in averager_test when optimization is turned on

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/c++_tools/statistics/Averager.cc

    r616 r620  
    66namespace statistics{
    77 
     8  Averager::Averager(void)
     9    : n_(0), x_(0), xx_(0)
     10  {
     11  }
    812
     13  Averager::Averager(double x, double xx, u_long n)
     14    : n_(n), x_(x), xx_(xx)
     15  {
     16  }
     17 
     18  Averager::Averager(const Averager& a)
     19    : n_(a.n_), x_(a.x_), xx_(a.xx_)
     20  {
     21  }
     22 
    923  const Averager& Averager::operator+=(const Averager& a)
    1024  {
  • trunk/c++_tools/statistics/Averager.h

    r616 r620  
    2424    /// Default constructor
    2525    ///
    26     inline Averager(void) : n_(0), x_(0), xx_(0) {}
     26    Averager(void);
    2727   
    2828    ///
     
    3030    /// number of samples \a n.
    3131    ///
    32     inline Averager(const double x,const double xx,const long n)
    33       : n_(n), x_(x), xx_(xx) {}
     32    Averager(double x, double xx, u_long n);
    3433
    3534    ///
    3635    /// Copy constructor
    3736    ///
    38     inline Averager(const Averager& a) : n_(a.n_), x_(a.x_), xx_(a.xx_) {}
     37    Averager(const Averager& a);
    3938
    4039    ///
    4140    /// Adding \a n (default=1) number of data point(s) with value \a d.
    4241    ///
    43     inline void add(const double d,const long n=1) { n_+=n; x_+=n*d; xx_+=n*d*d;}
     42    inline void add(double d, u_long n=1) { n_+=n; x_+=n*d; xx_+=n*d*d;}
    4443
    4544    ///
     
    5150    ///
    5251    template <typename T>
    53     void  add_values(const T& v, const long n=1);
     52    void  add_values(const T& v, u_long n=1);
    5453
    5554    ///
     
    6160    /// @return Number of data points
    6261    ///
    63     inline unsigned long  n(void) const { return n_; }
     62    inline u_long n(void) const { return n_; }
    6463
    6564    ///
     
    8988    /// @return Standard deviation around \a m, root of the variance(m).
    9089    ///
    91     inline double std(const double m) const { return sqrt(variance(m)); }
     90    inline double std(double m) const { return sqrt(variance(m)); }
    9291
    9392    ///
     
    111110    /// @return Variance when the mean is known to be \a m.
    112111    ///
    113     inline double variance(const double m) const
     112    inline double variance(double m) const
    114113    { return n_ ? (xx_ - 2*m*x_ + m*m*n()) /n_ : 0; }
    115114
     
    151150
    152151  private:
    153     unsigned long n_;
     152    u_long  n_;
    154153    double  x_, xx_;
    155154  };
     
    157156  // Template implementations
    158157  template <typename T>
    159   void  Averager::add_values(const T& v, const long n)
     158  void  Averager::add_values(const T& v, u_long n)
    160159  {
    161160    for (size_t i=0; i<v.size(); i++)
  • trunk/test/averager_test.cc

    r617 r620  
    5151  }
    5252
    53   Averager* a1 = new Averager(1+3+5,1+9+25,3);
     53
     54  Averager* a1 = new Averager(1.0+3+5,1.0+9+25,3);
    5455  if (!equal(a,*a1)){
    5556    ok=false;
    56     std::cout << a.sum_x() << std::endl;
    57     std::cout << a.sum_xx() << std::endl;
    58     std::cout << a.n() << std::endl;
    59     std::cout << a.variance() << std::endl;
    60     std::cout << a.mean() << std::endl;
    61     std::cout << a1->sum_x() << std::endl;
    62     std::cout << a1->sum_xx() << std::endl;
    63     std::cout << a1->n() << std::endl;
    64     std::cout << a1->variance() << std::endl;
    65     std::cout << a1->mean() << std::endl;
     57    std::cout.precision(25);
     58    std::cout << equal(a,*a1) << std::endl;
     59    std::cout << a.sum_x() << '\t' << a1->sum_x() << std::endl;
     60    std::cout << a.sum_xx() << '\t' << a1->sum_xx() << std::endl;
     61    std::cout << a.n() << '\t' << a1->n() << std::endl;
     62    std::cout << a.variance() << '\t' << a1->variance() << std::endl;
     63    std::cout << a.mean() << '\t' << a1->mean() << std::endl;
    6664    std::cout << a.mean() - a1->mean() << std::endl;
    6765    std::cout << a.variance() - a1->variance() << std::endl;
    68     *error << "error: Averager(const double x,const double xx,const long n)\n";
     66    *error << "error: Averager(double x, double xx, u_long n)\n";
    6967  }
    7068  delete a1;
     
    208206bool equal(const Averager& a, const Averager& b)
    209207{
    210 //  std::cout << (a.n()==b.n()) << std::endl;
     208//  std::cout << (a.n()==b.n()) << std::endl;
    211209//  std::cout << (a.mean()==b.mean()) << std::endl;
    212210//  std::cout << (a.variance()==b.variance()) << std::endl;
Note: See TracChangeset for help on using the changeset viewer.