math.exp(complex)

Raymond Hettinger vze4rx4y at verizon.net
Sun Sep 21 16:24:04 EDT 2003


[David Eppstein]
> Why doesn't this work?
>
> >>> import math
> >>> math.exp(1j*math.pi)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: can't convert complex to float; use e.g. abs(z)
>
> The expected answer, of course, is -1.
>
> It does work if you do it like this:
>
> >>> math.e**(1j*math.pi)
> (-1+1.2246467991473532e-16j)
>
> So why not math.exp(complex)?

Use the cmath module for complex ops:


>>> import cmath
>>> import math
>>> cmath.exp(1j*math.pi)

(-1+1.2246063538223773e-016j)


Raymond Hettinger






More information about the Python-list mailing list