我从以下几行代码中得到了错误TypeError: unsupported operand type(s) for -: 'Approximate' and 'float'
(整个脚本见下文,可能不需要,但可能会有帮助):
x = receivers[i][0] - ax.xx
y = receivers[i][1] - ay.xx
其中ax
和ay
是Approximate
类的对象。我知道为什么会出错,但我不确定我做错了什么。令人惊讶的是,我在某某或其他地方找不到任何解决这种情况的方法
完全错误:
Traceback (most recent call last):
File "example.py", line 98, in <module>
localize(1,1,1,1,1,1,1,1,1)
File "example.py", line 75, in localize
x = receivers[i][0] - Approximate.xx
TypeError: unsupported operand type(s) for -: 'Approximate' and 'float'
import math
from dataclasses import dataclass
@dataclass
class Approximate:
xi: float; dx: float
xf: float; n : float
ei: float = 0.0
ee: float = 0.0
xx: float = 0.0
stop: bool = False
done: bool = False
xs: float = None
def step(self):
if (self.ei < 0) or (self.ei > self.ee): # Better solution
self.ei = self.ee
self.xs = self.xx
if (self.stop): # Increase accuracy
if (self.i >= self.n):
self.done = True
self.xx = self.xs
return
self.xi = self.xs - self.dx
self.xf = self.xs + self.dx
self.xx = self.xi
self.dx *= 0.1
self.xi += self.dx
self.xf -= self.dx
self.stop = False
else:
self.xx += self.dx
if (self.xx > self.xf):
self.xx = self.xf
self.stop = True
yield
def localize(ax, ay, ta, bx, by, tb, cx, cy, tc):
ax = Approximate(1,1,1,1)
ay = Approximate(1,1,1,1)
receivers = [[ax, ay, ta],
[bx, by, tb],
[cx, cy, tc]]
dt = []
x = None
y = None
c = 299792458
for step in ax.step():
for step in ay.step():
for i in range(3):
x = receivers[i][0] - ax.xx
y = receivers[i][1] - ay.xx
a = math.sqrt((x*x) + (y*y))
dt[i] = a / c
a = dt[0]
for i in range(1, 3):
if(a > dt[i]):
a = dt[i]
for i in range(3):
dt[i] -= a
ax.ee = 0.0
ay.ee = 0.0
for i in range(3):
ax.ee += math.fabs(receivers[i][2] - d[i])
ay.ee += math.fabs(receivers[i][2] - d[i])
localize(1,1,1,1,1,1,1,1,1)
如果您真的想从
Approximate
类中减去一个浮点,那么您需要实现一个减法方法-这是通过__sub__
方法完成的。比如:或,试试
x = receivers[i][0].xx - ax.xx
(如果你是这个意思的话)相关问题 更多 >
编程相关推荐