A (very) few UI improvements
[eigenmath-fx.git] / choose.cpp
blob6b8293016d2031c7f2c94794e5ed543cd7782e01
1 // For example, the number of five card hands is choose(52,5)
2 //
3 // n!
4 // choose(n,k) = -------------
5 // k! (n - k)!
7 #include "stdafx.h"
8 #include "defs.h"
10 void
11 eval_choose(void)
13 push(cadr(p1));
14 eval();
15 push(caddr(p1));
16 eval();
17 choose();
20 // Result vanishes for k < 0 or k > n. (A=B, p. 19)
22 #define N p1
23 #define K p2
25 void
26 choose(void)
28 save();
30 K = pop();
31 N = pop();
33 if (choose_check_args() == 0) {
34 push_integer(0);
35 restore();
36 return;
39 push(N);
40 factorial();
42 push(K);
43 factorial();
45 divide();
47 push(N);
48 push(K);
49 subtract();
50 factorial();
52 divide();
54 restore();
57 int
58 choose_check_args(void)
60 if (isnum(N) && lessp(N, zero))
61 return 0;
62 else if (isnum(K) && lessp(K, zero))
63 return 0;
64 else if (isnum(N) && isnum(K) && lessp(N, K))
65 return 0;
66 else
67 return 1;