我想用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