scipy/comb-by-factorials
| Rule | comb-by-factorials |
|---|---|
| Pack | SciPy |
| Severity | warning |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
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.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
$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).Browse the other rules of the SciPy pack.