ZeroDivisionError:浮点除以零

时间:2020-04-24 23:26:18

标签: python-2.7

我不知道这是怎么回事,它以前工作过。 它从来没有发生在我身上。 有人帮忙,我迷路了。

错误行:

        result = p0 + (EMA - d0) / (d1 - d0) * (p1 - p0) if p0 != 100.0 or p1 != 100.0 else 100.0

我试试这个:

        if p0 != 100.0 or p1 != 100.0:
            result = p0 + (EMA - d0) / (d1 - d0) * (p1 - p0)
        else:
            result = 100.0

代码:

    def requestCurData(self, damageRating, movingAvgDamage):
        self.values = config.values[self.check_player_thread()][self.name]
        if len(self.values) == 4:
            tm = datetime.datetime.toordinal(datetime.datetime.utcnow()) - 1
            self.values.extend([tm, tm])
            config.values[self.check_player_thread()][self.name] = self.values
        if movingAvgDamage not in self.values or datetime.datetime.toordinal(datetime.datetime.utcnow()) >= self.values[5] + 1:
            p0 = self.values[2]
            d0 = self.values[3]
            t0 = self.values[5]
            p1 = damageRating
            d1 = movingAvgDamage
            t1 = datetime.datetime.toordinal(datetime.datetime.utcnow())
            self.values = [p0, d0, p1, d1, t0, t1]
            config.values[self.check_player_thread()][self.name] = self.values
        if self.values[0] == self.values[2] and self.values[1] == self.values[3]:
            self.values[3] += 10
            self.values[5] = datetime.datetime.toordinal(datetime.datetime.utcnow())
            config.values[self.check_player_thread()][self.name] = self.values
        EDn = self.battleDamage + max(self.RADIO_ASSIST, self.TRACK_ASSIST, self.STUN_ASSIST)
        k = 0.0198019801980198022206547392443098942749202251434326171875  # 2 / (100.0 + 1)
        EMA = k * EDn + (1 - k) * self.movingAvgDamage
        p0, d0, p1, d1, t0, t1 = self.values
        result = p0 + (EMA - d0) / (d1 - d0) * (p1 - p0) if p0 != 100.0 or p1 != 100.0 else 100.0
        nextMark = round(min(100.0, result), 2) if result > 0 else 0.0
        self.initiated = self.values[1] and not nextMark >= self.damageRating and not self.damageRating - nextMark > 3

1 个答案:

答案 0 :(得分:0)

该错误消息尚不清楚,但我想您可能正在尝试将浮点数除以整数,而Python不喜欢这样。试试:

result = p0 + (EMA - d0) / float( (d1 - d0) * (p1 - p0) ) if p0 != 100.0 or p1 != 100.0 else 100.0

如果这不起作用,则可能是您试图将其除以零(这是不可能的),因此请将计算结果包装在try-catch块中:

try:
   result = p0 + (EMA - d0) / (d1 - d0) * (p1 - p0) if p0 != 100.0 or p1 != 100.0 else 100.0
except ZeroDivisionError:
    result = "some default value"