e vs exp()?

Tim Peters tim.one at comcast.net
Fri Sep 20 15:33:25 EDT 2002


[Terry Reedy]
> ...
> On the hypothesis that x**y is calculated as exp(y*log(x)),

Bad hypothesis.  The implementation of a decent x**y function is one of the
most difficult tasks in a platform's math library.  The "naive" expression
you give *can* be used if the library has an efficient way to do the
internal computations using extended precision.  Else it's a disaster:  if
all computations are done in the result's precision, log(x) introduces an
error of its own, then that error is effectively multipled by y, and then
the magnified error in that is used as an exponent!  As a rule of thumb, the
number of trailing bits in the result that are complete nonsense can be as
bad as the number of bits in the internal float exponent field (this is
specific to using that specific expression, of course).  However, a decent
modern libm supplies an x**y with error strictly less than 1 unit in the
last bit.  I've written such a beast without benefit of an extended hardware
precision; I wouldn't want to do it again <wink>.

WRT exp(y) vs math.e**y, math.e isn't the mathematical e, of course, and
whatever representation error math.e suffers for cramming e into 53 bits is
magnified by the "**y" part.  exp(y) is going to be more accurate, unless
the platform libm plain sucks (Ping Tak Peter Tang published a way to
compute exp(y) that's very efficient, uses only native precision, and has
worst-case error less than 0.53 units in the last place; I had the extreme
pleasure of collaborating with him on a math library in a previous life).





More information about the Python-list mailing list