math.exp(complex)

Tim Peters tim.one at comcast.net
Sun Sep 21 18:42:19 EDT 2003


[Tim]
>> Math libraries in general can return a better result for exp(x) than
>> e**x, though, since the latter form uses a machine approximation to e
>> as the base.

[David Eppstein]
> Doesn't look especially different to me, for the numbers I'm currently
> interested in applying this to (small pure imaginary):
>
> >>> from cmath import *
> >>> exp(1j*pi)
> (-1+1.2246467991473532e-16j)
> >>> e**(1j*pi)
> (-1+1.2246467991473532e-16j)

There are few shortcuts in numerical analysis.  If you want to understand,
and estimate how bad it *might* get over various input ranges, you'll have
to do careful analysis of the implementations.  You'll probably find that
very hard, because the Python implementations build on the platform C's
sin(), cos(), exp(), atan2(), pow() and hypot(), and few C vendors document
the accuracy of their implementations -- you don't have the raw material
then to estimate the accuracy and vulnerable areas of Python's
implementations.  For that reason, I can't tell you anything that's
generally true across implementations.  But you need only glance at c_exp in
cmathmodule.c, and c_pow in compleobject.c, to convince yourself that
c_exp() has far fewer places it can get into numerical trouble (it's a much
simpler algorithm).






More information about the Python-list mailing list