scipy/comb-by-factorials
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| SciPy | warning | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
factorial overflows to inf above 170, so a binomial coefficient built from factorials gives inf or nan for larger numbers. Call special.comb.
ways = special.factorial(n) / (special.factorial(k) * special.factorial(n - k))ways = special.comb(n, k)What the rule reports
Section titled “What the rule reports”The rule reports special.factorial(n) / (special.factorial(k) * special.factorial(n - k)), and
the same with the factors in another order or divided one at a time. factorial overflows to inf
above 170, so at n = 200 and k = 3 the ratio gave nan. comb lost no digits at the same n and k.
How to fix it
Section titled “How to fix it”xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.
The unsafe fix writes special.comb(n, k). factorial imported by name is fixed only in a file that imports comb from scipy.special.
For k > n the ratio can give infinity, and for n = -1 it can give NaN. comb gives zero in both cases.
Diagnostic message
Section titled “Diagnostic message”$F overflows to inf above 170, so this ratio is inf or nan once $N passes 170. At 200 and 3 that lost all 16 digits where comb lost none. Write $COMB($N, $K).See all rules in the SciPy pack.