[Numpy-tickets] [NumPy] #640: Index assignment into pre-allocated array does silent type cast, causing user confusion (or worse)

NumPy numpy-tickets@scipy....
Sat Jan 5 08:03:52 CST 2008


#640: Index assignment into pre-allocated array does silent type cast, causing
user confusion (or worse)
------------------------+---------------------------------------------------
 Reporter:  brorson     |        Owner:  somebody
     Type:  defect      |       Status:  new     
 Priority:  normal      |    Milestone:  1.0.5   
Component:  numpy.core  |      Version:  none    
 Severity:  normal      |   Resolution:          
 Keywords:              |  
------------------------+---------------------------------------------------
Comment (by brorson):

 In response to my above note, as well as the discussion on numpy-discuss,
 I'll change my opinion.   Instead of doing a typecast, NumPy should throw
 an exception *whenever* any information is about to be lost (i.e.
 truncating complex to real).  Here's two examples, one in which NumPy does
 the right thing, and one in which is does the wrong thing:

 Right (throw exception):

 In [10]: A = numpy.zeros([3, 3])

 In [11]: A[1, 1] = 1+2j
 ---------------------------------------------------------------------------
 <type 'exceptions.TypeError'>             Traceback (most recent call
 last)

 /fs/home/sdb/<ipython console> in <module>()

 <type 'exceptions.TypeError'>: can't convert complex to float; use
 abs(z)



 Wrong (silent truncation):

 In [12]: B = 10*numpy.random.rand(3, 3) + 1j*numpy.random.rand(3, 3)

 In [13]: B[1, 1]
 Out[13]: (7.15013107181+0.383220559014j)

 In [14]: A[1, 1] = B[1, 1]

 In [15]:

-- 
Ticket URL: <http://scipy.org/scipy/numpy/ticket/640#comment:2>
NumPy <http://projects.scipy.org/scipy/numpy>
The fundamental package needed for scientific computing with Python.


More information about the Numpy-tickets mailing list