python - ValueError: negative number cannot be raised to a fractional power -


when tried in terminal

>>> (-3.66/26.32)**0.2 

i got following error

traceback (most recent call last):   file "<stdin>", line 1, in <module> valueerror: negative number cannot raised fractional power 

however, able in 2 steps like,

>>> (-3.66/26.32) -0.13905775075987842 >>> -0.13905775075987842 ** 0.2 -0.6739676327771593 

why behaviour? way solve in single line?

raising power takes precedence on unary minus sign.

so have -(0.13905775075987842 ** 0.2) , not (-0.13905775075987842) ** 0.2 expect:

>>> -0.13905775075987842 ** 0.2 -0.6739676327771593 >>> (-0.13905775075987842) ** 0.2 traceback (most recent call last):   file "<stdin>", line 1, in <module> valueerror: negative number cannot raised fractional power 

if want work should write (-3.66/26.32 + 0j)**0.2

>>> (-3.66/26.32 + 0j)**0.2 (0.5452512685753758+0.39614823506888347j) 

or switch python 3 noted @timpietzcker.


Comments