From: Eric S. Raymond Date: Fri, 14 Apr 1995 01:50:14 +0000 (+0000) Subject: Added verbose and gamma tests. X-Git-Tag: 2.0~9 X-Git-Url: http://thyrsus.com/gitweb/?a=commitdiff_plain;h=957f34eaa893527133fff90bdaa9567c12beba49;p=deal.git Added verbose and gamma tests. --- diff --git a/deal.c b/deal.c index 5360ef9..6ceccc5 100644 --- a/deal.c +++ b/deal.c @@ -1,6 +1,8 @@ /* * deal -- compute combination probabilities for card draws * + * ($Id$) + * * Based on the `cardprobs' program written by Jeremy C. York * in September 1993; cleanup, comments, * generalization, and interface improvements by Eric Raymond @@ -38,8 +40,14 @@ #define BASE_DEFAULT 7 /* # cards in initial deal */ #define TURNS_DEFAULT 20 /* show this many turns if total is greater */ +#define TRUE 1 +#define FALSE 0 + static int sx, sk, sM, sN; static int sa, sb; +static int verbose = FALSE; + +static double log_a_choose_b(int a, int b); static void hypercatch(int sig) { @@ -71,14 +79,15 @@ char **argv; { extern char *optarg; /* set by getopt */ extern int optind; /* set by getopt */ - double hyper_prob(); + static double hyper_prob(); int status, special, total, columns, i, j; int base = BASE_DEFAULT; int turns = TURNS_DEFAULT; - int headers = 1; + int gammatest = FALSE; + int headers = TRUE; - while ((status = getopt(argc, argv, "b:c:ht:")) != EOF) + while ((status = getopt(argc, argv, "b:c:ght:v")) != EOF) { switch (status) { @@ -90,14 +99,22 @@ char **argv; columns = atoi(optarg); break; + case 'g': + gammatest = TRUE; + break; + case 'h': - headers = 0; + headers = FALSE; break; case 't': turns = atoi(optarg); break; + case 'v': + verbose = TRUE; + break; + default: usage(); } @@ -109,6 +126,13 @@ char **argv; special = atoi(argv[optind]); total = atoi(argv[optind + 1]); + if (gammatest) { + double p = log_a_choose_b(special, total); + + (void) printf("log_a_choose_b(%d, %d) = %f\n", special, total, p); + exit(0); + } + if (total < special) { (void) printf("Can't have more specials than total cards!\n"); exit(-1); @@ -175,10 +199,10 @@ char **argv; * giving the final answer. */ -double hyper_prob(x, k, M, N) +static double hyper_prob(x, k, M, N) int x, k, M, N; { - double exp(), log_a_choose_b(); + double exp(); /* for the signal handler */ sx = x; @@ -190,21 +214,25 @@ double hyper_prob(x, k, M, N) log_a_choose_b(N, k))); } -double log_a_choose_b(a,b) +static double log_a_choose_b(a,b) int a, b; { - double lgamma(); + double p, lgamma(); sa = a; sb = b; signal(SIGFPE, choosecatch); - return(lgamma((double) (a+1)) - lgamma((double) (b+1)) - - lgamma((double) (a-b+1))); + p = lgamma((double) (a+1)) - lgamma((double) (b+1)) - + lgamma((double) (a-b+1)); + + if (verbose) + (void) fprintf(stderr, "log_a_choose_b(%d, %d) = %f\n", a, b, p); signal(SIGFPE, hypercatch); + return(p); } /* deal.c ends here */