Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

python - Raise to 1/3 gives complex number

I cannot understand the following output. I would expect Numpy to return -10 (or an approximation). Why is it a complex number?

print((-1000)**(1/3.))

Numpy answer

(5+8.660254037844384j)

Numpy official tutorial says the answer is nan. You can find it in the middle of this tutorial.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are exponentiating a regular Python scalar rather than a numpy array.

Try this:

import numpy as np

print(np.array(-1000) ** (1. / 3))
# nan

The difference is that numpy does not automatically promote the result to a complex type, whereas a Python 3 scalar gets promoted to a complex value (in Python 2.7 you would just get a ValueError).

As explained in the link @jonrsharpe gave above, negative numbers have multiple cube roots. To get the root you are looking for, you could do something like this:

x = -1000
print(np.copysign(np.abs(x) ** (1. / 3), x))
# -10.0

Update 1

Mark Dickinson is absolutely right about the underlying cause of the problem - 1. / 3 is not exactly the same as a third because of rounding error, so x ** (1. / 3) is not quite the same thing as the cube root of x.

A better solution would be to use scipy.special.cbrt, which computes the 'exact' cube root rather than x ** (1./3):

from scipy.special import cbrt

print(cbrt(-1000))
# -10.0

Update 2

It's also worth noting that versions of numpy >= 0.10.0 will have a new np.cbrt function based on the C99 cbrt function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...