[Numpy-discussion] Is this a bug in repr ?

Mark Sienkiewicz sienkiew at stsci.edu
Tue Mar 15 12:20:52 EDT 2011

The usual expectation is that (when possible) repr() returns a value 
that you can eval() to get the original data back.  But,

 >>> from numpy import *
 >>> a = array( [  16.50698631688883822 ] )
 >>> b = eval(repr(a))
 >>> a-b
array([ -3.11116111e-09])
 >>> import numpy.testing
 >>> numpy.testing.assert_array_almost_equal(a,b,decimal=15)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/stsci/pyssgdev/2.7/numpy/testing/utils.py", line 775, in 
    header=('Arrays are not almost equal to %d decimals' % decimal))
  File "/usr/stsci/pyssgdev/2.7/numpy/testing/utils.py", line 618, in 
    raise AssertionError(msg)
Arrays are not almost equal to 15 decimals

(mismatch 100.0%)
 x: array([ 16.50698632])
 y: array([ 16.50698632])

I noticed this because a bunch of tests failed exactly this way.  Of 
course, the problem is that assert_array_almost_equal does not print 
with the precision that it compared, which in turn happens because it 
just uses repr() to convert the array.

I would expect that repr would print the values at least to the 
resolution that they are stored, so I think this is a bug.

This happens with the current trunk of numpy in python 2.7 on Red Hat 
Enterprise linux in 32 and 64 bits, and on Macintosh Leopard in 32 
bits.  I did not try any other configuration.


More information about the NumPy-Discussion mailing list