[Tutor] Little problem with math module

ALAN GAULD alan.gauld at btinternet.com
Mon Apr 21 19:50:51 CEST 2008


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 think about it :-)

> 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080421/c7947b40/attachment.htm 


More information about the Tutor mailing list