-3

I don't know if I am implementing this correctly. I created a function called boosted that returns the float for the given value but I am getting this error.

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

Can someone explain why I am getting this and how to get it to work? I've provided my teacher's website http://staffwww.fullcoll.edu/aclifton/cs133/assignment5.html and my code for the function.

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);
}
  • are you permitted by your teacher to have SO do your homework for you? – mikep Nov 04 '17 at 06:18
  • `v.size()/65536` will perform integer division, so unless you have more than 65536 elements in `v`, `numWords` will be 0. `c2 = pow(numWords - v[i], 2)/numWords;` will result in a division by zero, which is not a number. – user4581301 Nov 04 '17 at 06:22
  • Interesting fun fact, `pow` is designed to handly nasty computations like e to the power of pi, and is often vast overkill for squaring a number and very slow because of it. You are almost always better off performing the multiplication yourself. – user4581301 Nov 04 '17 at 06:28
  • They're teaching Boost in school??? – txtechhelp Nov 04 '17 at 06:31
  • @mikep -- are you doing my homework for me though? The error I am getting is with my teacher's predefined code that I don't understand how to use. You aren't writing the rest of the code for me. – straightson Nov 04 '17 at 06:32
  • @txtechhelp probably should teach it some time. Very useful stuff. I WISH it existed when I was in school. Not for school mind you. I had to do everything in Java last time I was in school. – user4581301 Nov 04 '17 at 06:36
  • @txtechhelp -- no we are only using it this one time. It was never mentioned in class so I have questions on why my implementation is not working – straightson Nov 04 '17 at 06:39
  • Most likely cause is the divide by zero I outlined above. That's a guess mind you. Can't give a full and proper answer without a [mcve]. – user4581301 Nov 04 '17 at 06:42

1 Answers1

1

I see a couple issues with the code:

  1. integer division: float numWords = v.size()/65536;
  2. c2 is supposed to the summation of (expected - hashes[i])/expected

Try this:

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

Also, numWords should be renamed to expected (or something better).

Mark Mucha
  • 1,560
  • 2
  • 12
  • 18