有没有办法在Python中定义浮点数组?

有没有办法在Python中定义浮点数组?,python,arrays,numpy,physics,astronomy,Python,Arrays,Numpy,Physics,Astronomy,在我的天文学作业中,我需要模拟行星绕太阳的椭圆轨道。为此,我需要使用for循环来重复计算行星的运动。但是,每次尝试运行该程序时,都会出现以下错误: RuntimeWarning: invalid value encountered in power r=(x**2+y**2)**1.5 Traceback (most recent call last): File "planetenstelsel3-4.py", line 25, in <module> ax[i] = a(x[i]

在我的天文学作业中,我需要模拟行星绕太阳的椭圆轨道。为此,我需要使用for循环来重复计算行星的运动。但是,每次尝试运行该程序时,都会出现以下错误:

RuntimeWarning: invalid value encountered in power
r=(x**2+y**2)**1.5
Traceback (most recent call last):
File "planetenstelsel3-4.py", line 25, in <module>
ax[i] = a(x[i],y[i])*x[i]
ValueError: cannot convert float NaN to integer
前几行只是一些起始参数


提前谢谢你的帮助

当你进行物理模拟时,你绝对应该对所有事情都使用浮动
0
在Python中是一个整数常量,因此
np.tile
创建整数数组;使用
0.0
作为
np.tile
的参数来执行浮点数组;或者最好使用
np.zero(N)

您可以从其
dtype
成员中检查任何数组变量的数据类型:

>>> np.tile(0, 10).dtype
dtype('int64')
>>> np.tile(0.0, 10).dtype
dtype('float64')
>>> np.zeros(10)
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
>>> np.zeros(10).dtype
dtype('float64')

您需要
x=np.zero(N)
,等等:这将数组声明为浮点数组


这是在数组中放入零的标准方法(
np.tile()
便于使用固定数组创建平铺)。

python中的int只受内存限制,而
是浮动的。从查看代码看,我不明白为什么,但是由于某种原因,您的
a(x,y)
函数正在接收x或y的
nan
值。是的,您可能应该将数组定义为浮点数。如果您使用的是tile,那么只需指定
0即可。
而不是
0
作为重复值:
x=np.tile(0,N)
>>> np.tile(0, 10).dtype
dtype('int64')
>>> np.tile(0.0, 10).dtype
dtype('float64')
>>> np.zeros(10)
array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])
>>> np.zeros(10).dtype
dtype('float64')