numpy/expm1-log1p
| Rule | expm1-log1p |
|---|---|
| Pack | NumPy |
| Severity | warning |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
np.exp(x) - 1 and np.log(1 + x) lose digits to rounding when x is near 0. Use np.expm1 and np.log1p.
log_survival = np.log(1 - p)log_survival = np.log1p(-p)What the rule reports
Section titled “What the rule reports”The rule reports np.exp(x) - 1, 1 - np.exp(x), np.log(1 + x), np.log(x + 1) and np.log(1 - p), but not on a number literal.
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 np.expm1(x), -np.expm1(x), np.log1p(x), np.log1p(-p).
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
exp($X) - 1 loses digits to rounding when $X is near 0. Write $NP.expm1($X).Browse the other rules of the NumPy pack.