From 317aae3a5363777554eae423753449f73f2a968c Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Thu, 13 Apr 1995 14:35:01 +0000 Subject: [PATCH] Proper signal handling. --- deal.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/deal.c b/deal.c index bc427a9..80634e5 100644 --- a/deal.c +++ b/deal.c @@ -15,7 +15,7 @@ * * B(M, i) * B(N-M, k-i) * P(X = i) = --------------------- - * B(N, k) + * B(N, k) * * where B(a,b) is read "a choose b", and is the number of ways * to choose b objects out of a possible objects. The formula for it is @@ -27,9 +27,21 @@ * cc cardprobs.c -lm -o cardprobs */ #include +#include #define BASEHAND 7 /* # cards in initial deal */ +static int sx, sk, sM, sN; + +void catcher(int sig) +{ + putchar('\n'); + if (sig == SIGFPE) + (void) printf("Died while computing H(%d, %d, %d, %d)\n", sx,sk,sM,sN); + else + (void) printf("Caught signal %d\n", sig); +} + main(argc, argv) int argc; char **argv; @@ -60,6 +72,8 @@ main(argc, argv) (void) printf("%d specials out of %d total; %d turns, %d columns\n", special, total, turns, columns); + (void) signal(SIGFPE, catcher); + (void) printf(" | %% chance of this number of specials\n"); (void) printf("Turn | "); for (j = 0; j < columns; j++) @@ -72,7 +86,7 @@ main(argc, argv) for (i = 1; i <= turns; i++) { (void) printf("%2d | ",i); - for (j = 0; (j < columns) && (j < i + 8) && (j < special+1); j++) + for (j = 0; j < columns && j <= i + BASEHAND && j <= special; j++) { double p = hyper_prob(j, i + BASEHAND, special, total); @@ -97,6 +111,12 @@ double hyper_prob(x, k, M, N) { double exp(), log_a_choose_b(); + /* for the signal handler */ + sx = x; + sk = k; + sM = M; + sN = N; + return(exp(log_a_choose_b(M,x) + log_a_choose_b(N-M,k-x) - log_a_choose_b(N, k))); } -- 2.43.0