]> thyrsus.com Git - deal.git/commitdiff
Change the test args.
authorEric S. Raymond <esr@thyrsus.com>
Fri, 14 Apr 1995 13:55:40 +0000 (13:55 +0000)
committerEric S. Raymond <esr@thyrsus.com>
Fri, 14 Apr 1995 13:55:40 +0000 (13:55 +0000)
deal.c
deal.xml

diff --git a/deal.c b/deal.c
index 6ceccc55fb213086d1fdfece7648f0d2dab7c226..5a630f0514009a734c6fd796532655bc809abada 100644 (file)
--- a/deal.c
+++ b/deal.c
@@ -1,7 +1,7 @@
 /*
  * deal -- compute combination probabilities for card draws
  *
- * ($Id$)
+ * ($Id: deal.c,v 1.18 1995/04/14 01:50:14 esr Exp esr $)
  *
  * Based on the `cardprobs' program written by Jeremy C. York
  * <jeremy@stat.cmu.edu> in September 1993; cleanup, comments, 
@@ -43,7 +43,7 @@
 #define TRUE   1
 #define FALSE  0
 
-static int sx, sk, sM, sN;
+static int si, sk, sM, sN;
 static int sa, sb;
 static int verbose = FALSE;
 
@@ -53,7 +53,7 @@ static void hypercatch(int sig)
 {
     putchar('\n');
     if (sig == SIGFPE)
-       (void) printf("Died while computing hyper(%d, %d, %d, %d)\n", sx,sk,sM,sN);
+       (void) printf("Died while computing hyper(i=%d, k=%d, M=%d, N=%d)\n", si,sk,sM,sN);
     else
        (void) printf("Caught signal %d during hyper computation\n", sig);
 }
@@ -85,9 +85,11 @@ char **argv;
     int base = BASE_DEFAULT;
     int turns = TURNS_DEFAULT;
     int gammatest = FALSE;
+    int hypertest = FALSE;
+    int choosetest = FALSE;
     int headers = TRUE;
 
-    while ((status = getopt(argc, argv, "b:c:ght:v")) != EOF)
+    while ((status = getopt(argc, argv, "b:c:ht:vBGH")) != EOF)
     {
        switch (status)
        {
@@ -99,14 +101,22 @@ char **argv;
            columns = atoi(optarg);
            break;
 
-       case 'g':
+       case 'G':
            gammatest = TRUE;
            break;
 
+       case 'H':
+           hypertest = TRUE;
+           break;
+
        case 'h':
            headers = FALSE;
            break;
 
+       case 'B':
+           choosetest = TRUE;
+           break;
+
        case 't':
            turns = atoi(optarg);
            break;
@@ -120,13 +130,34 @@ char **argv;
        }
     }  
 
+    if (gammatest) {
+       double p = lgamma(atoi(argv[optind]));
+
+       (void) printf("lgamma(%d) = %f\n", atoi(argv[optind]), p);
+       exit(0);
+    }
+
+    if (hypertest) {
+       double p;
+
+       si = atoi(argv[optind]);
+       sk = atoi(argv[optind + 1]);
+       sM = atoi(argv[optind + 2]);
+       sN = atoi(argv[optind + 3]);
+
+       p = hyper_prob(si, sk, sM, sN);
+
+       (void) printf("hyper(%d, %d, %d, %d) = %f\n", si, sk, sM, sN, p);
+       exit(0);
+    }
+
     if (optind + 2 > argc)
        usage();
 
     special = atoi(argv[optind]);
     total = atoi(argv[optind + 1]);
 
-    if (gammatest) {
+    if (choosetest) {
        double p = log_a_choose_b(special, total);
 
        (void) printf("log_a_choose_b(%d, %d) = %f\n", special, total, p);
@@ -199,19 +230,20 @@ char **argv;
  * giving the final answer.
  */
 
-static double hyper_prob(x, k, M, N)
-    int x, k, M, N;
+static double hyper_prob(i, k, M, N)
+    int i, k, M, N;
 {
     double exp();
 
     /* for the signal handler */
-    sx = x;
+    si = i;
     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)));
+    return(exp(log_a_choose_b(M,i)
+           + log_a_choose_b(N-M,k-i)
+           - log_a_choose_b(N, k)));
 }
 
 static double log_a_choose_b(a,b)
index 5c982979f6dd7c56601ab18e8f96cc1e56afd818..7cf7fc829b9354085952363113a218e2a07b4135 100644 (file)
--- a/deal.xml
+++ b/deal.xml
@@ -53,10 +53,19 @@ Verbose.  Log the computation steps for each hypergeometric
 coefficient to standard error.  Use this for debugging if you are
 a trained numerical analyst (kids, don't try this yourselves at home!).
 .TP 10
--g
+-B
+Binomial coefficient test.  Compute and print the binomial coefficient
+B(n, k) or `n choose k'.
+.TP 10
+-G
 Gamma function test.  Compute and display the log of the gamma
-function of the two arguments.  If -v tells you that log_a_choose_b()
-is blowing up, this will enable you to probe the local lgamma function.
+function of the argument.  If -v tells you that the computation of
+B(n, k) is blowing up, this will enable you to probe the local lgamma
+function.
+.TP
+-H
+Compute the hypergeometric coefficient H(i, k, M, N) with arguments
+supplied from the command line.  For debugging.
 .PP
 .SH EXAMPLES
 To compute the probabilities for drawing your 3 Hurloon