[Numpy-discussion] Warning or error on conversion from complex to float.

Michael McNeil Forbes mforbes at physics.ubc.ca
Thu Nov 8 13:32:42 EST 2007


Hi,

Is it possible or easy to add a warning and/or error when array  
assignments are made that lose information?  I just got caught with  
the following type of code:

def f():
     return numpy.array([1j,2.0])

x = numpy.empty((2,),dtype=float)
x[:] = f()

I am pre-allocating arrays for speed, but made a change to f()  
resulting in complex results.  My code now silently ignores the  
imaginary part.  It would have been very helpful if the assignment  
check to make sure that the conversion was not going to lose  
information (presumably with an assert so that performance is not  
affected outside of debugging).

Any simple suggestions on how to avoid this problem in the future?   
How hard would it be to add such a check in numpy?

Thanks,
Michael.



More information about the NumPy-Discussion mailing list