Skip to content

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)

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.

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).

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.