我正在尝试使用 python 的数学模块 (math.log(x,[base]) 计算对数,但是当我使用 float(raw_input) 时x 和基值,它给我错误,ZeroDivisionError: float division by zero。
x = 9.0
base = 3.0
请您参考如下方法:
废话,完美运行
>>> import math
>>> x=float(raw_input())
9.0
>>> base=float(raw_input())
3.0
>>> math.log(x, base)
2.0
您为什么不向我们展示您是如何重现该问题的呢? wim 非常正确 - 1 的基础会报错
>>> base=float(raw_input())
1.0
>>> math.log(x, base)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: float division by zero
另一方面 - 如果 x<=0你得到一个“数学域错误”
>>> x=float(raw_input())
0
>>> math.log(x, base)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
