IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 15, 2023, 12:09:10 PM (3 years ago)
Author:
eugene
Message:

psBinomialCoeff had an error : the multiplicative formula was using an integer value to carry the intermediate results, but those were sometimes fractional

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20230313/psLib/src/math/psBinomialCoeff.c

    r36222 r42494  
    2222  if (k == 0) { return (b); }
    2323
     24  psS64 num = 1;
     25  psS64 den = 1;
     26
    2427  for (i = 1; i <= k; i++) {
    25     b *= (N - (k - i))/i;
     28
     29      // old version: b *= (N - (k - i))/i;
     30      // wikipedia recursion version: b *= (N + 1 - i)/i;
     31
     32      // the old version just counts the elements in the opposite order from the wikipedia version:
     33
     34      // numerators would be, e.g., for C(6,4):
     35      // old  version (6 - (4 - 1)), (6 - (4 - 2)), (6 - (4 - 3)), (6 - (4 - 4)) = 3, 4, 5, 6
     36      // wiki version (6 + 1 - 1), (6 + 1 - 2), (6 + 1 - 3), (6 + 1 - 4) = 6, 5, 4, 3
     37
     38      // denominators would be the same: 1, 2, 3, 4.
     39
     40      // since these are multiplied, the order of the numerators is independent of the order of the denominators
     41
     42      // but both of these formulae are bad because the factors can be fractional, but that means we cannot use an
     43      // integer to carry the intermediate result. 
     44
     45      // instead, we need to calculate the integer product of the denominators and the integer
     46      // product of the numerators and divide at the end
     47     
     48      num *= (N + 1 - i);
     49      den *= i;
    2650  }
     51
     52  b = num / den;
    2753
    2854  return(b);
Note: See TracChangeset for help on using the changeset viewer.