A (very) few UI improvements
[eigenmath-fx.git] / binomial.cpp
blob952d036ea81e435fbce09b64fa3f22d0c2e52fcf
1 // Binomial coefficient
2 //
3 // Input: tos-2 n
4 //
5 // tos-1 k
6 //
7 // Output: Binomial coefficient on stack
8 //
9 // binomial(n, k) = n! / k! / (n - k)!
11 // The binomial coefficient vanishes for k < 0 or k > n. (A=B, p. 19)
13 #include "stdafx.h"
14 #include "defs.h"
15 static void ybinomial(void);
16 static int check_args(void);
18 void
19 eval_binomial(void)
21 push(cadr(p1));
22 eval();
23 push(caddr(p1));
24 eval();
25 binomial();
29 void
30 binomial(void)
32 save();
33 ybinomial();
34 restore();
37 #define N p1
38 #define K p2
40 static void
41 ybinomial(void)
43 K = pop();
44 N = pop();
46 if (check_args() == 0) {
47 push(zero);
48 return;
51 push(N);
52 factorial();
54 push(K);
55 factorial();
57 divide();
59 push(N);
60 push(K);
61 subtract();
62 factorial();
64 divide();
67 static int
68 check_args(void)
70 if (isnum(N) && lessp(N, zero))
71 return 0;
72 else if (isnum(K) && lessp(K, zero))
73 return 0;
74 else if (isnum(N) && isnum(K) && lessp(N, K))
75 return 0;
76 else
77 return 1;