11-对数函数
16.3.2 对数函数
基本的对数函数是 Math.log 。在一些编程语言中,“log”就表示“以10为底的对数”,“ln”则表示“自然对数”,所以要记住在JavaScript中,“log”表示“自然对数”。ES6中为了方便计算,引入了 Math.log10 。表16-2所列为对数函数。
| 函 数 | 描 述 | 例 子 | | :----- | :----- | :----- | :----- | :----- | | Math.log(x) | x的自然对数 | Math.log(Math.E) // 1 | Math.log(17.5) // ~2.86 | | Math.log10(x) | 以10为底的x的对数,等于Math.log(x)/ Math.log(10) | Math.log10(10) // 1 | Math.log10(16.7) // ~1.22 | | Math.log2(x) | 以2为底的x的对数,等于Math.log(x)/ Math.log(2) | Math.log2(2) // 1 | Math.log2(5) // ~2.32 | | Math.log1p(x) | 1+x的自然对数,等于 Math.log(1 + x) | Math.log1p(Math.E - 1) // 1 | Math.log1p(17.5) // ~2.92 |