If I use np.log(0)
, I get a warning like:
>>> 1*(np.log(0) + 6.28318530718j)
__main__:1: RuntimeWarning: divide by zero encountered in log
__main__:1: RuntimeWarning: invalid value encountered in cdouble_scalars
(-inf+nan*j)
I would advise against trying to "work with" inf
and nan
. You can change the behaviour of numpy using numpy.seterr
:
>>> np.seterr(divide='raise')
{'over': 'warn', 'divide': 'raise', 'invalid': 'warn', 'under': 'ignore'}
>>> 1*(np.log(0) + 6.28318530718j)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FloatingPointError: divide by zero encountered in log
Now you can catch the FloatingPointError
exception and deal with it in some useful way.
Note that the nan
part of original answer is actually a side effect of the inf
. If you do:
>>> 1*(2 + 6.28318530718j)
(2+6.28318530718j)
If one part of the multiplication has no complex component, it doesn't create a nan
in the other side.