我收到错误<string>:149:RuntimeWarning:生成列表时在sqrt中遇到无效值

Bob*_*ger 8 python numpy sqrt

def ellipse(numPoints, genX=np.linspace, HALF_WIDTH=10, HALF_HEIGHT=6.5):
    xs = 10.*genX(-1,1,numPoints)
    ys = 6.5*np.sqrt(1-(xs**2))
    return(xs, ys, "-")
Run Code Online (Sandbox Code Playgroud)

我收到一个错误,指出在squareroot中遇到无效值.我看不出它是什么.

sqrt(0) = 0
6.5*sqrt(1- (-1**2)) = 0
Run Code Online (Sandbox Code Playgroud)

他们应该工作,但y值有问题,他们正在返回"南"

Foo*_*ser 7

可能xs**2返回一个数字> 1sqrt,负数将返回nan(不是数字)

>>> import numpy as np
>>> np.sqrt(-1)
nan
Run Code Online (Sandbox Code Playgroud)

如果我是对的numpy提供了复数函数,我认为这是表示sqrt(x)的唯一方法,其中x <0

  • 如果你想让numpy在这种情况下给出错误,你可以使用`np.seterr(all ='raise')`并给出`FloatingPointError:在sqrt`中遇到的无效值而不是`nan`.更多信息,请访问http://docs.scipy.org/doc/numpy/user/misc.html#how-numpy-handles-numerical-exceptions (2认同)