对数
math.log(x) 给出 x 的自然(基础 e)对数。
math.log(math.e) # 1.0
math.log(1) # 0.0
math.log(100) # 4.605170185988092
由于浮点数的限制,math.log 可能会丢失精度,数字接近 1。为了准确计算接近 1 的日志,使用 math.log1p,它评估 1 的自然对数加上参数:
math.log(1 + 1e-20) # 0.0
math.log1p(1e-20) # 1e-20
math.log10 可用于原木基地 10:
math.log10(10) # 1.0
Python 2.x >= 2.3.0
当使用两个参数时,math.log(x, base) 给出了 x 中给定 x 的对数(即 log(x) / log(base)。
math.log(100, 10) # 2.0
math.log(27, 3) # 3.0
math.log(1, 10) # 0.0