]> thyrsus.com Git - deal.git/commitdiff
Proper signal handling.
authorEric S. Raymond <esr@thyrsus.com>
Thu, 13 Apr 1995 14:35:01 +0000 (14:35 +0000)
committerEric S. Raymond <esr@thyrsus.com>
Thu, 13 Apr 1995 14:35:01 +0000 (14:35 +0000)
deal.c

diff --git a/deal.c b/deal.c
index bc427a923f64e8710a7a5c024397bc0025d473e6..80634e529c14c904b55b7ef3a6d2e60f7134074a 100644 (file)
--- 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
  *     cc cardprobs.c -lm -o cardprobs
  */
 #include <math.h>
+#include <signal.h>
 
 #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)));
 }