2017-11-04 33 views
-3

我不知道我是否正确实现了这一点。我创建了一个名为boosted的函数,它返回给定值的浮点数,但我得到这个错误。我不知道如何使用提升,虽然我的老师确实提供了代码,但我得到一个错误

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); 
} 
+0

你的老师允许你做你的功课吗? – mikep

+0

'v.size()/ 65536'将执行整数除法,所以除非'v'中有超过65536个元素,'numWords'将会是0.'c2 = pow(numWords - v [i],2)/ numWords;'会导致零除,这不是一个数字。 – user4581301

+0

有趣的有趣的事实是,'pow'被设计为像pi这样的趾高气扬的计算,并且通常是大量矫枉过正的数字,因为它很慢。你几乎总是更喜欢自己进行乘法运算。 – user4581301

回答

1

我看到一对夫妇的问题与代码:

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

的总和,试试这个:

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(或更好的东西)。

相关问题