我想用python (math.log(x,[base])
的数学模块计算对数,但是当我使用float(raw_input)
作为x和基准值时,它给了我错误ZeroDivisionError: float division by zero
。用Python计算对数
x = 9.0
base = 3.0
我想用python (math.log(x,[base])
的数学模块计算对数,但是当我使用float(raw_input)
作为x和基准值时,它给了我错误ZeroDivisionError: float division by zero
。用Python计算对数
x = 9.0
base = 3.0
您应该避免x < = 0,因为它没有被定义!
这不会返回不同类型的错误吗? – jamylak 2012-03-27 04:26:06
是的,那是我的想法,但我该如何避免这种情况?如果只想要3而不是3.1,该怎么办。 – user1294592 2012-03-27 04:26:23
是的,它应该是一个溢出错误,如果你的x值在一个循环中改变,那么你需要用if语句处理这个case,否则base = 3.1应该不成问题 – Timoun 2012-03-27 04:27:00
废话,它工作得很好
>>> 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
在otherhand - 如果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
什么串那样你输入? – Taymon 2012-03-27 04:19:31
同时粘贴实际的代码和回溯 – agf 2012-03-27 04:21:06
是给我们一个你的输入为x和基准值的例子 – jamylak 2012-03-27 04:21:43