用Python计算对数

use*_*592 1 python logarithm

我试图使用python的数学模块计算对数(math.log(x,[base]),但是当我使用float(raw_input)x和base值时,它会给我错误,ZeroDivisionError: float division by zero.

x = 9.0
base = 3.0
Run Code Online (Sandbox Code Playgroud)

Joh*_*ooy 7

胡说八道,它运作得非常好

>>> import math
>>> x=float(raw_input())
9.0
>>> base=float(raw_input())
3.0
>>> math.log(x, base)
2.0
Run Code Online (Sandbox Code Playgroud)

你为什么不向我们展示你如何重现这个问题呢?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
Run Code Online (Sandbox Code Playgroud)

另一方面 - 如果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
Run Code Online (Sandbox Code Playgroud)