I have started learning Python recently and I've been going through the NumPy official quickstart guide which includes this example for iterating.
>>> a
array([-1000, 1, -1000, 27, -1000, 125, 216, 343, 512,
729])
>>> for i in a:
... print(i**(1/3.))
...
nan
1.0
nan
3.0
nan
5.0
6.0
7.0
8.0
9.0
However, if I just try to raise -1000 to the power of (1/3.) outside of the loop it returns a value.
>>> -1000**(1/3.)
-9.999999999999998
With parentheses around -1000 it also returns a value.
>>> (-1000)**(1/3.)
(5+8.660254037844384j)
Why is it that the same action returns nan
in the for loop? I'm using Python 3.6.3 :: Anaconda custom (64-bit). I also tried with different fractions that do not round up and it's the same. With a fraction that rounds up to .0 it works though.
I couldn't find a similar question. Excuse me if I'm missing something very obvious.
Edit: A few comments mentioned that the question duplicates NumPy, RuntimeWarning: invalid value encountered in power and it's true, the problem was I didn't see such an error. The discussion there, however, seems to include a few possible workarounds.