diff options
| author | Christoph Berg <myon@debian.org> | 2025-09-16 12:37:12 +0200 |
|---|---|---|
| committer | Christoph Berg <myon@debian.org> | 2025-09-16 12:57:29 +0200 |
| commit | 5a4967c577ad245a2117b7b2f71c13b1212fe285 (patch) | |
| tree | 4d09343cfe757239895a0dd9b399242d2a07d7c2 /lib/ftrsd/ftrsd_paper/binomial.c | |
| parent | 23c903a09fb1d73fd20b50323cd80cfb3aba8799 (diff) | |
Merge 2.7 changes, I had uploaded -1 from the wrong branch.debian/3.0.0_rc1+repack-2
Diffstat (limited to 'lib/ftrsd/ftrsd_paper/binomial.c')
| -rwxr-xr-x | lib/ftrsd/ftrsd_paper/binomial.c | 138 |
1 files changed, 69 insertions, 69 deletions
diff --git a/lib/ftrsd/ftrsd_paper/binomial.c b/lib/ftrsd/ftrsd_paper/binomial.c index fcbdafd9..c4180250 100755 --- a/lib/ftrsd/ftrsd_paper/binomial.c +++ b/lib/ftrsd/ftrsd_paper/binomial.c @@ -1,69 +1,69 @@ -#include <stdio.h> -#include <limits.h> - -/* Original code copied from - http://rosettacode.org/wiki/Evaluate_binomial_coefficients -*/ - -/* We go to some effort to handle overflow situations */ - -static unsigned long gcd_ui(unsigned long x, unsigned long y) { - unsigned long t; - if (y < x) { t = x; x = y; y = t; } - while (y > 0) { - t = y; y = x % y; x = t; /* y1 <- x0 % y0 ; x1 <- y0 */ - } - return x; -} - -unsigned long binomial(unsigned long n, unsigned long k) { - unsigned long d, g, r = 1; - if (k == 0) return 1; - if (k == 1) return n; - if (k >= n) return (k == n); - if (k > n/2) k = n-k; - for (d = 1; d <= k; d++) { - if (r >= ULONG_MAX/n) { /* Possible overflow */ - unsigned long nr, dr; /* reduced numerator / denominator */ - g = gcd_ui(n, d); nr = n/g; dr = d/g; - g = gcd_ui(r, dr); r = r/g; dr = dr/g; - if (r >= ULONG_MAX/nr) return 0; /* Unavoidable overflow */ - r *= nr; - r /= dr; - n--; - } else { - r *= n--; - r /= d; - } - } - return r; -} - -int main() { - - //Get test results - printf("%lu\n", binomial(5, 3)); // 10 - printf("%lu\n", binomial(40, 19)); // 131282408400 - printf("%lu\n", binomial(67, 31)); // 11923179284862717872 - - // Compute special cases for paper on TF soft-decision RS decoder: - double a,b,c,p; - a=(double)binomial(40, 35); - b=(double)binomial(23, 5); - c=(double)binomial(63, 40); - p=a*b/c; - printf("%e %e %e %e\n",a,b,c,p); - - a=(double)binomial(40, 36); - b=(double)binomial(23, 4); - c=(double)binomial(63, 40); - p=a*b/c; - printf("%e %e %e %e\n",a,b,c,p); - - a=(double)binomial(40, 37); - b=(double)binomial(23, 8); - c=(double)binomial(63, 45); - p=a*b/c; - printf("%e %e %e %e\n",a,b,c,p); - return 0; -} +#include <stdio.h>
+#include <limits.h>
+
+/* Original code copied from
+ http://rosettacode.org/wiki/Evaluate_binomial_coefficients
+*/
+
+/* We go to some effort to handle overflow situations */
+
+static unsigned long gcd_ui(unsigned long x, unsigned long y) {
+ unsigned long t;
+ if (y < x) { t = x; x = y; y = t; }
+ while (y > 0) {
+ t = y; y = x % y; x = t; /* y1 <- x0 % y0 ; x1 <- y0 */
+ }
+ return x;
+}
+
+unsigned long binomial(unsigned long n, unsigned long k) {
+ unsigned long d, g, r = 1;
+ if (k == 0) return 1;
+ if (k == 1) return n;
+ if (k >= n) return (k == n);
+ if (k > n/2) k = n-k;
+ for (d = 1; d <= k; d++) {
+ if (r >= ULONG_MAX/n) { /* Possible overflow */
+ unsigned long nr, dr; /* reduced numerator / denominator */
+ g = gcd_ui(n, d); nr = n/g; dr = d/g;
+ g = gcd_ui(r, dr); r = r/g; dr = dr/g;
+ if (r >= ULONG_MAX/nr) return 0; /* Unavoidable overflow */
+ r *= nr;
+ r /= dr;
+ n--;
+ } else {
+ r *= n--;
+ r /= d;
+ }
+ }
+ return r;
+}
+
+int main() {
+
+ //Get test results
+ printf("%lu\n", binomial(5, 3)); // 10
+ printf("%lu\n", binomial(40, 19)); // 131282408400
+ printf("%lu\n", binomial(67, 31)); // 11923179284862717872
+
+ // Compute special cases for paper on TF soft-decision RS decoder:
+ double a,b,c,p;
+ a=(double)binomial(40, 35);
+ b=(double)binomial(23, 5);
+ c=(double)binomial(63, 40);
+ p=a*b/c;
+ printf("%e %e %e %e\n",a,b,c,p);
+
+ a=(double)binomial(40, 36);
+ b=(double)binomial(23, 4);
+ c=(double)binomial(63, 40);
+ p=a*b/c;
+ printf("%e %e %e %e\n",a,b,c,p);
+
+ a=(double)binomial(40, 37);
+ b=(double)binomial(23, 8);
+ c=(double)binomial(63, 45);
+ p=a*b/c;
+ printf("%e %e %e %e\n",a,b,c,p);
+ return 0;
+}
|
