math.exp(complex)

David Eppstein eppstein at ics.uci.edu
Sun Sep 21 20:05:15 EDT 2003


In article <mailman.1064184209.18938.python-list at python.org>,
 "Tim Peters" <tim.one at comcast.net> wrote:

> [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).

Ok, thanks for the advice.  Looks like exp is the right choice unless 
there's some strong reason to use powering instead (which there isn't in 
my situation).

-- 
David Eppstein                      http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science




More information about the Python-list mailing list