[Python-Dev] compex numbers (was Floor Division)

Jim Jewett jimjjewett at gmail.com
Tue Jan 23 20:47:26 CET 2007


Tim Peters wrote:

>  complex_new() ends with:

>  	cr.real -= ci.imag;
>  	cr.imag += ci.real;

> and I have no idea what that thinks it's doing.  Surely this isn't intended?!:

I think it is.  python.org/sf/1642844 adds comments to make it less unclear.

>  >>> complex(complex(1.0, 2.0), complex(10.0, 20.0))
>  (-19+12j)

>  WTF?

    >>> help(complex)
    Help on class complex in module __builtin__:

    class complex(object)
     |  complex(real[, imag]) -> complex number
     |
     |  Create a complex number from a real part and an optional imaginary part.
     |  This is equivalent to (real + imag*1j) where imag defaults to 0.

If "real" and "imag" are themselves complex numbers, then normalizing
the result will move the imaginary portion of the "real" vector into
the imaginary part and vice versa.

Note that changing this (to discard the imaginary parts) would break
passing complex numbers to their own constructor.

    >>> a=complex(1,2)
    >>> b=complex(a)
    >>> a==b
    True

-jJ


More information about the Python-Dev mailing list