Problem with complex numbers

Matthias Götz matthias.goetz.23 at googlemail.com
Sat Mar 22 22:37:45 CET 2008


Hello python fans,

I have a small problem using python and complex math.

The pow(complex,complex) function in the windows python version doesn't have
the same semantic as the source version.

(I have downloaded :

   -

   Python 2.5.2 compressed source
tarball<http://www.python.org/ftp/python/2.5.2/Python-2.5.2.tgz>(for
Unix or OS X compile)
   - Python 2.5.2 Windows
installer<http://www.python.org/ftp/python/2.5.2/python-2.5.2.msi>

)

If found in Complex.py:

    def __pow__(self, n, z=None):
        if z is not None:
            raise TypeError, 'Complex does not support ternary pow()'
        if IsComplex(n):
            if n.im:
                if self.im: raise TypeError, 'Complex to the Complex power'
                else: return exp(math.log(self.re)*n)
            n = n.re
        r = pow(self.abs(), n)
        phi = n*self.angle()
        return Complex(math.cos(phi)*r, math.sin(phi)*r)

which mean for example if i calculate z1 power z2 (z1,z2 are complex
numbers),
and z1 and z2 has imaginary parts not equals zero,
i must get an TypeError. Is that right?

But i tried that in the IDLE python shell by doing this

>>> z1=complex(1,2)
>>> z2=complex(2,3)
>>> z1**z2
(-0.01513267242272265-0.1798674839133349j)
>>>

and this is everything unlike an TypeError.

So if somebody can help me, it would be nice. Thanks.

Matze

(Sorry for bad english, I'am german)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080322/f434c902/attachment.html>


More information about the Python-list mailing list