Ignore:
Timestamp:
Aug 20, 2010, 5:51:00 AM (13 years ago)
Author:
Peter
Message:

prefer casting to double first and then multiply rather than the opposite. fixes #638

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/0.6-stable/yat/statistics/Fisher.cc

    r2119 r2315  
    3030
    3131#include <algorithm>
     32#include <cassert>
    3233
    3334namespace theplu {
     
    6465  void Fisher::expected(double& a, double& b, double& c, double& d) const
    6566  {
    66     double N = a_+b_+c_+d_;
     67    // use floting point arithmetic to avoid overflow
     68    double N = static_cast<double>(a_) + static_cast<double>(b_)
     69      + static_cast<double>(c_) + static_cast<double>(d_);
    6770    a =((a_+b_)*(a_+c_)) / N;
    6871    b =((a_+b_)*(b_+d_)) / N;
     
    98101    d_ = d;
    99102
    100     oddsratio_=static_cast<double>(a*d)/static_cast<double>(b*c);
     103    oddsratio_= (static_cast<double>(a) / static_cast<double>(b) *
     104                 static_cast<double>(d) / static_cast<double>(c) );
    101105    return oddsratio_;
    102106  }
     
    118122      return p_value_approximative()/2.0;
    119123    }
     124    // check for overflow
     125    assert(c_ <= c_+d_ && d_ <= c_+d_);
     126    assert(a_ <= a_+b_ && b_ <= a_+b_);
     127    assert(a_ <= a_+c_ && c_ <= a_+c_);
     128
    120129    return statistics::cdf_hypergeometric_P(c_, c_+d_, a_+b_, a_+c_);
    121130  }
     
    147156  }
    148157
    149 
    150158}}} // of namespace statistics, yat, and theplu
Note: See TracChangeset for help on using the changeset viewer.