我不知道如何使用 boost,尽管我的老师确实提供了代码,但我遇到了错误

I don't know how to use boost and although my teacher did provide the code I am getting an error

我不知道我是否正确实施了这一点。我创建了一个名为 boosted 的函数,该函数 returns 给定值的浮点数,但出现此错误。

terminate called after throwing an instance of 

    'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::domain_error> >'
      what():  Error in function boost::math::cdf(const chi_squared_distribution<double>&, double): Chi Square parameter was -nan, but must be > 0 !
    Aborted

谁能解释一下为什么我会得到这个以及如何让它工作?我提供了我老师的网站 http://staffwww.fullcoll.edu/aclifton/cs133/assignment5.html 以及我的函数代码。

float boosted (vector <int>& v){
    float c2 = 0;
    float numWords = v.size()/65536;
    for(int i = 0; i < v.size(); i++)
        c2 = pow(numWords - v[i], 2)/numWords;

    boost::math::chi_squared c2d(65535.0);
    return boost::math::cdf(c2d, c2);
}

我发现代码有几个问题:

  1. 整数除法:float numWords = v.size()/65536;
  2. c2 应该是 (expected - hashes[i])/expected
  3. 的总和

试试这个:

 float boosted (vector <int>& v){
   float c2 = 0;
   float numWords = v.size()/65536.0;
   for(int i = 0; i < v.size(); i++)
       c2 += pow(numWords - v[i], 2)/numWords;

   boost::math::chi_squared c2d(65535.0);
   return boost::math::cdf(c2d, c2);
 }

此外,numWords 应重命名为 expected(或更好的名称)。