Why am I getting a positive result with fractional powers?

The following output from IDLE doesn't make any sense to me.

>>> a=-1.0
>>> a**(1/3)
1.0
>>> a==-1.0
True
>>> -1.0**(1/3)
-1.0

      

Why do two theoretically equivalent operators return different results? How does Python (2.7) handle the method __pow__

to double, what is the result? I just tried it with integers and got the same result. Apart from calculating the sign of the function entry __pow__

and copying it to the result, how can I fix this?

+3


source to share


1 answer


This is an operator constraint problem:

>>> -1.0**(1/3)
-1.0
>>> (-1.0)**(1/3)
1.0

      



Also note that it (1/3)

is zero if you are not importing division

from __future__

, which gives the behavior of Python 3.x (and ValueError

). Use 1/3.

to get 1/3 as float.

+6


source







All Articles