/*
* deal -- compute combination probabilities for card draws
*
+ * ($Id$)
+ *
* Based on the `cardprobs' program written by Jeremy C. York
* <jeremy@stat.cmu.edu> in September 1993; cleanup, comments,
* generalization, and interface improvements by Eric Raymond
#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)
{
{
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)
{
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();
}
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);
* 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;
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 */