Python的math.log(1000, 10)结果为2.9999999999999996,如下:
在Python官方文档中的说明:
math.log
(x[, base])
With one argument, return the natural logarithm of x (to base e).
With two arguments, return the logarithm of x to the given base, calculated aslog(x)/log(base)
.math.log10
(x)
Return the base-10 logarithm of x. This is usually more accurate thanlog(x,
10)
.
math.log(x) 是以自然底数 e 来计算,所以 math.log(math.e)=1.0,math.log(x, b) = math.log(x) / math.log(b)。而 math.log10(x) 是直接以 10 为底进行计算的。
在两种不同的计算方式下,中间结果的近似和浮点数运算引入额外的误差也不尽相同。