mirror of
https://github.com/saitohirga/WSJT-X.git
synced 2024-11-26 22:28:41 -05:00
Make binomial_subs.c OK on 32-bit machines.
git-svn-id: svn+ssh://svn.code.sf.net/p/wsjt/wsjt/branches/wsjtx@6217 ab8295b8-cf94-4d9e-aec4-7959e3be5d79
This commit is contained in:
parent
5606d8a3aa
commit
036d408908
@ -7,8 +7,8 @@
|
||||
|
||||
/* We go to some effort to handle overflow situations */
|
||||
|
||||
static unsigned long gcd_ui(unsigned long x, unsigned long y) {
|
||||
unsigned long t;
|
||||
static unsigned long long gcd_ui(unsigned long long x, unsigned long long y) {
|
||||
unsigned long 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 */
|
||||
@ -16,18 +16,18 @@ static unsigned long gcd_ui(unsigned long x, unsigned long y) {
|
||||
return x;
|
||||
}
|
||||
|
||||
unsigned long binomial(unsigned long n, unsigned long k) {
|
||||
unsigned long d, g, r = 1;
|
||||
unsigned long long binomial(unsigned long long n, unsigned long long k) {
|
||||
unsigned long 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 */
|
||||
if (r >= ULONG_LONG_MAX/n) { /* Possible overflow */
|
||||
unsigned long 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 */
|
||||
if (r >= ULONG_LONG_MAX/nr) return 0; /* Unavoidable overflow */
|
||||
r *= nr;
|
||||
r /= dr;
|
||||
n--;
|
||||
@ -39,7 +39,7 @@ unsigned long binomial(unsigned long n, unsigned long k) {
|
||||
return r;
|
||||
}
|
||||
|
||||
unsigned long binomial_(int *n, int *k)
|
||||
unsigned long long binomial_(int *n, int *k)
|
||||
{
|
||||
// printf("n=%d k=%d %lu\n",*n,*k,binomial(*n,*k));
|
||||
return binomial(*n,*k);
|
||||
|
Loading…
Reference in New Issue
Block a user