[Numpy-tickets] [NumPy] #519: allclose fails with inf

NumPy numpy-tickets@scipy....
Sun May 13 21:29:23 CDT 2007

#519: allclose fails with inf
 Reporter:  qwerty      |       Owner:  somebody    
     Type:  defect      |      Status:  new         
 Priority:  normal      |   Milestone:              
Component:  numpy.core  |     Version:  1.0.1       
 Severity:  normal      |    Keywords:  allclose inf
 allclose(x,y, rtol, atol)

 | x - y | < atol + rtol * | y |

 This formula is good so long as x and y do not contain inf (-inf) at the
 same index.  In such a case, the difference is not well-defined.  The
 problem then, is that allclose fails and returns False when it should be
 returning True.

 >>> a = array([log(0)])
 >>> b = array([log(0)])
 >>> a == b
 array([True], dtype=bool)
 >>> allclose(a,b)

 alltrue(a==b) will work if you have an array of inf...but this is
 definitely undesirable when you comparing floats.  Somehow, it would be
 nice if the following returned True instead of False.

 >>> a = array([log(0), log(.5)])
 >>> b = array([log(0), log(.5000000001)])
 >>> allclose(a,b)

 Eck. Here is an uninformed solution:

 pruned_x = []
 pruned_y = []
 for xx, yy in zip(x,y):
   if xx != yy:
 return old_allclose(array(pruned_x), array(pruned_y))

 This means we'd need an additional run through the values, but this
 certainly makes results more predictable...and prevents the operator from
 having to check for cases like these.

Ticket URL: <http://projects.scipy.org/scipy/numpy/ticket/519>
NumPy <http://projects.scipy.org/scipy/numpy>
The fundamental package needed for scientific computing with Python.

More information about the Numpy-tickets mailing list