我知道这个错误是什么意思,但我不知道为什么我的程序中会出现这个错误,我似乎无法让我的程序工作。
这是我得到的错误:
ZeroDivisionError: float division by zero
这是我的代码的一小段:
calc_A = round((math.log(y2) - math.log(y1))/(math.log(x2) - math.log(x1)), -1)
calc_B = round(y1/pow(calc_A, x1), -1)
以下是使用的值:
- x1: 1
- y1: 10
- x2: 2
- y2:20
最佳答案
calc_A
为 0.0
,0
的任意幂(0 除外)仍为 0
。
因此,y1/0
引发此异常。
你的问题是你告诉了 round()
四舍五入到最接近的 10
倍数(因为您将 ndigits
参数设置为 -1
)。
>>> x1=1
>>> y1=10
>>> x2=2
>>> y2=20
>>> math.log(x2) - math.log(x1)
0.6931471805599453
>>> math.log(y2) - math.log(y1)
0.693147180559945
>>> (math.log(y2) - math.log(y1))/(math.log(x2) - math.log(x1))
0.9999999999999996
>>> round(_, -1)
0.0
>>> round(0.99)
1.0
关于Python 零除错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27078726/