I need to write a function to calculate the (exp(x) - 1) / x
, taking into the account the fact that as x
approaches 0
the function approaches 1
. I have the following implementation
def f(x):
return np.where(x == 0, 1.0, np.expm1(x) / x)
This is correct in the sense that it returns correct values, but the annoying thing is that when it is called with an x
argument that contains zeros, it issues a RuntimeWarning: invalid value encountered in true_divide
. I understand that this is because the division by zero actually happens in the third argument of where
although those nan
values are never used.
Is there a better implementation or a way to avoid the annoying warning?