<br><div class="gmail_quote">On Mon, Apr 21, 2008 at 12:07 AM, Alan Gauld <<a href="mailto:alan.gauld@btinternet.com">alan.gauld@btinternet.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
>>> pow(20, 0.3333333)<br>
2.7144173455393048<br>
>>> pow(-20, 0.3333333)<br>
Traceback (most recent call last):<br>
File "<stdin>", line 1, in <module><br>
ValueError: negative number cannot be raised to a fractional power<br>
>>> -20**0.3333333<br>
-2.7144173455393048<br>
>>><br>
<br>
So the exponentiation operator seems happy but pow isn't.<br>
<br>
Very strange and potentially a bug in pow()?<br>
Unless someone knows a reason for it?</blockquote><div><br><br>I think you're confusing the order of operations.<br><br>math.pow(-20, (1.0/3.0)) and -20**(1.0/3.0) are not equivalent<br><br>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)<br>
</div></div><br>The behavior of ** is appropriate since <a href="http://docs.python.org/ref/summary.html">http://docs.python.org/ref/summary.html</a> shows that exponentiation has higher precedence over positive, negative.<br>
<br>Also, I found this from <a href="http://www.python.org/download/releases/2.0/">http://www.python.org/download/releases/2.0/</a>:<br><br><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">
<i>Raise ZeroDivisionError when raising zero to a negative number,
e.g. 0.0 ** -2.0. 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. On Linux, it raises a ValueError.</i></blockquote><div><br>which is related to why some people would get nan and others a ValueError.<br><br>And, just to clarify (although i doubt there was any confusion over it), -20**(1/3) = -1 * (20**0) = -1 due to integer division magic.<br>
</div>