# [Tutor] Little problem with math module

Andreas Kostyrka andreas at kostyrka.org
Tue Apr 22 17:00:32 CEST 2008

Hmmm, the power function has basically two definitions:

a ** n

for integer n, pow is defined for all a in R.
for real n, pow is defined only for non-negative a.

If you think how a**x is derived (as a generalization from a**(p/q)),
that makes sense.

Basically, a ** (p/q), p > 0, q > 0, gcd(p, q) = 1, is defined as the
q-th root of a ** p. Now for all even q, there is no result in R, only q
results in C.

Now, trying to remember my highschool math, a**x, x in R is defined
basically by interpolation of the the a**x, x in Q function. As a**x
with x in Q is only defined as a real function for a >= 0, so is a**x, x
in R.

In practice a ** x can be also derived from the exp and ln functions:
(which are basically exp(x) = e ** x, and ln is the logarithm to the
base e, funnily, the math module calls that log, which is often used by
mathematicians for the logarithm to base 10, at least in German)

math.exp(x * math.log(a)) == a ** x

(this can be expressed with any base, but for deeper reasons it's
usually e=2.7182... that is used for a base)

Ok, hope that I didn't put my foot in my mouth, considering that high
school is over a decade in that past for me ;)
At least it's probably offtopic for the python tutor list :)

Andreas

Am Montag, den 21.04.2008, 17:50 +0000 schrieb ALAN GAULD:
> On Mon, Apr 21, 2008 at 12:07 AM, Alan Gauld
> <alan.gauld at btinternet.com> wrote:
>
>
> >>> pow(-20, 0.3333333)
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> ValueError: negative number cannot be raised to a fractional power
> >>> -20**0.3333333
> -2.7144173455393048
> >>>
>
>
> I think you're confusing the order of operations.
>
> math.pow(-20, (1.0/3.0)) and -20**(1.0/3.0) are not equivalent
>
> Whereas, as john mentioned, -20**(1.0/3.0) is actually
> -(20**(1.0/3.0)), math.pow(-20, (1.0/3.0)) is (-20)**(1.0/3.0)
>
>
> Yes, quite correct, I posted my reply before the others had
> posted theirs although it showed up afterwards(at least on gmane)
>
> > exponentiation has higher precedence over positive, negative.
>
> Yep, I hadn't realized that, although it does make sense when you
>
> > Note that math.pow is unrelated to the builtin power operator
> > and the result of math.pow(0.0, -2.0) will vary by platform.
>
> This is interesting, I had assumed that pow simply called **.
> Does anyone know why they made it different?
>
> Alan G.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
URL: <http://mail.python.org/pipermail/tutor/attachments/20080422/e52554cf/attachment.pgp>