我收到错误< string>:149:RuntimeWarning:在生成列表时在sqrt中遇到无效值 [英] I'm getting an error <string>:149: RuntimeWarning: invalid value encountered in sqrt while generating a list

查看:540
本文介绍了我收到错误< string>:149:RuntimeWarning:在生成列表时在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, "-")

我收到一条错误消息,指出在平方根中遇到了无效的值.我看不到它是什么.

I am getting an error that states that an invalid value was encountered in a squareroot. I can't see what it is.

sqrt(0) = 0
6.5*sqrt(1- (-1**2)) = 0

它们应该工作,但是y值有问题,它们返回"nan"

They should work, but the y values are having problems, they are returning "nan"

推荐答案

可能xs**2返回数字> 1具有负数的sqrt将返回nan(不是数字)

probably xs**2 returns a number > 1 sqrt with negative number will return nan (not a number)

>>> import numpy as np
>>> np.sqrt(-1)
nan

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

If i am right numpy provides complex numbers functionality which i think is the only way to represent sqrt(x) where x<0

这篇关于我收到错误&lt; string&gt;:149:RuntimeWarning:在生成列表时在sqrt中遇到无效值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆