thouis / numpy-trac-migration

numpy Trac to github issues migration
2 stars 3 forks source link

np.log gives RuntimeWarning for arrays with nan values when array is float32 but not float64 (Trac #2161) #5953

Open numpy-gitbot opened 12 years ago

numpy-gitbot commented 12 years ago

Original ticket http://projects.scipy.org/numpy/ticket/2161 on 2012-06-15 by trac user cmorton, assigned to unknown.

I can easily suppress the warning by changing np.seterr, but it seemed like there shouldn't be a different result between the two.

>>> import numpy as np
>>> np.seterr(all='raise')
>>> np.log(np.array([1, np.e, np.e**2, 0, np.nan]).astype(np.float64))
array([  0.,   1.,   2., -inf])
>>>np.log(np.array([1, np.e, np.e**2, 0, np.nan]).astype(np.float32))
Traceback (most recent call last):
  File "<pyshell#57>", line 1, in <module>
    np.log(np.array([1, np.e, np.e**2, 0]).astype(np.float32))
FloatingPointError: divide by zero encountered in log

Sorry if this ticket is missing something, it is the first one I have ever done.

numpy-gitbot commented 12 years ago

trac user cmorton wrote on 2012-06-15

I don't think my original code is totally correct, here is a simpler example:

>>> np.seterr(all='raise')
>>> np.log(np.array([np.nan]).astype(np.float64))
array([ nan])
>>> np.log(np.array([np.nan]).astype(np.float32))
Traceback (most recent call last):
  File "<pyshell#60>", line 1, in <module>
    np.log(np.array([np.nan]).astype(np.float32))
FloatingPointError: invalid value encountered in log
numpy-gitbot commented 12 years ago

trac user cmorton wrote on 2012-06-15

This seems like it might be related to Ticket #1360 and it is not just nan values

>>> np.log(np.array([0.0]).astype(np.float64))
array([-inf])
>>> np.log(np.array([0.0]).astype(np.float32))
Traceback (most recent call last):
  File "<pyshell#82>", line 1, in <module>
    np.log(np.array([0.0]).astype(np.float32))
FloatingPointError: divide by zero encountered in log

>>> np.log(np.array([-1.0]).astype(np.float64))
array([ nan])
>>> np.log(np.array([-1.0]).astype(np.float32))
Traceback (most recent call last):
  File "<pyshell#83>", line 1, in <module>
    np.log(np.array([-1.0]).astype(np.float32))
FloatingPointError: invalid value encountered in log