python math.log()求对数

log()语法

math.log(x[, base])

python源码中对log()方法的介绍:

   Return the logarithm of x to the given base.
    If the base not specified, returns the natural logarithm (base e) of x.

即,math.log()方法至少接收一个参数,可以用于计算以base参数为底的x的对数,如果没有指定base参数,默认情况下将以自然常数e为底。


参数

参数描述
x必须参数,real number,如int或float等类型,如果小于等于0,则抛出ValueError
base可选参数,real number,指定底数,大于0,且不等于1

提示:如果参数不是real number,python将抛出TypeError。


返回值

Python浮点数float类型值。


math.log()实例代码

>>> import math
>>> math.log(math.e)
1.0
>>> math.log(10,10)
1.0
>>> math.log(1,0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error
>>> math.log(10,1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: float division by zero
>>> math.log(0,10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error

全栈后端 / Python库 :









Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.