[Tutor] Little problem with math module

John Fouhy john at fouhy.net
Mon Apr 21 04:52:54 CEST 2008

On 21/04/2008, John Fouhy <john at fouhy.net> wrote:
>  >>> -1 ** (1.0/2.0)
>  -1.0

Replying to myself:

It turns out that all is not what it seems.  Let's try again:

>>> (-1)**0.5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: negative number cannot be raised to a fractional power

So, to revise my answer, for math.pow(x,y) or x**y, python will refuse
negative values of x.  This might not be mathematically perfect, but
you get what you pay for, I guess.

Furthermore, python evaluates "-x**y" as "-(x**y)" which is why it
seemed to give correct answers.


More information about the Tutor mailing list