Floating point exception in C -


#include <stdio.h> #include <math.h> #include <string.h>  long fctl(int n){   int a=1,i;   for(i=n;i>1;--i)     a*=i;   return a; }  long ch(int n, int r){   return fctl(n) / (fctl(n-r)*fctl(r)); }  int main(){   char *hands[] = {"onepair", "twopair", "triple", "straight", "flush", "fullhouse", "fourofakind", "straightflush", "royalflush"};   double handprobs[9];    handprobs[0] = ch(13,1)*ch(4,2) * ch(12,3)*pow(ch(4,1), 3) / ch(52,5);   handprobs[1] = ch(13,2)*pow(ch(4,2), 2) * ch(11,1)*ch(4,1) / ch(52,5);   handprobs[2] = ch(13,1)*ch(4,3) * ch(12,2)*pow(ch(4,1), 2) / ch(52,5);   handprobs[3] = 10.0 * pow(ch(4, 1),5) / ch(52, 5)  -  10.0/ch(52,5) - 4.0/ch(52,5);   handprobs[4] = ch(13,5)*ch(4,1) / ch(52, 5) - 10.0/ch(52,5);   handprobs[5] = ch(13,1)*ch(4,3) * ch(12,1)*ch(4,2) / ch(52,5);   handprobs[6] = ch(13,1)*1 * ch(12,1)*ch(4,1) / ch(52,5);   handprobs[7] = 40.0 / ch(52, 5) - 4.0/ch(52,5),   handprobs[8] = 4.0 / ch(52, 5);   int i;   for(i=0;hands[i];++i){     printf("%s\t%f\n",hands[i], handprobs[i]);   } } 

when compile returns "floating point exception (core dumped)", not sure why. (have tried converting probs (double).) ideas?

fctl(52) waaaaay big int. you're going have rethink approach doing calculation. can output int_max see how far can go. can buy tiny bit more space using unsigned long long (cf. ullong_max) still near big enough 52! .

invoking integer overflow causes undefined behaviour; "floating point exception" means attempt integer division zero, plausible given attempted calculations plus fact overflowed. don't ask me why reported fpe despite fact didn't involve floating point. (probably "historical reasons")


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -