"pow" (power) function
uymqlp502 at sneakemail.com
Thu Mar 16 02:58:45 CET 2006
Ben Cartwright wrote:
> Russ wrote:
> > Does "pow(x,2)" simply square x, or does it first compute logarithms
> > (as would be necessary if the exponent were not an integer)?
> The former, using binary exponentiation (quite fast), assuming x is an
> int or long.
> If x is a float, Python coerces the 2 to 2.0, and CPython's float_pow()
> function is called. This function calls libm's pow(), which in turn
> uses logarithms.
I just did a little time test (which I should have done *before* my
original post!), and 2.0**2 seems to be about twice as fast as
pow(2.0,2). That seems consistent with your claim above.
I'm a bit surprised that pow() would use logarithms even if the
exponent is an integer. I suppose that just checking for an integer
exponent could blow away the gain that would be achieved by avoiding
logarithms. On the other hand, I would think that using logarithms
could introduce a tiny error (e.g., pow(2.0,2) = 3.9999999996 <- made
up result) that wouldn't occur with multiplication.
> > Does "x**0.5" use the same algorithm as "sqrt(x)", or does it use some
> > other (perhaps less efficient) algorithm based on logarithms?
> The latter, and that algorithm is libm's pow(). Except for a few
> special cases that Python handles, all floating point exponentation is
> left to libm. Checking to see if the exponent is 0.5 is not one of
> those special cases.
I just did another little time test comparing 2.0**0.5 with sqrt(2.0).
Surprisingly, 2.0**0.5 seems to take around a third less time.
None of these differences are really significant unless one is doing
super-heavy-duty number crunching, of course, but I was just curious.
Thanks for the information.
More information about the Python-list