Movatterモバイル変換


[0]ホーム

URL:


  1. 面向开发者的 Web 技术
  2. JavaScript
  3. JavaScript 参考
  4. JavaScript 标准内置对象
  5. Math
  6. Math.log()

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in EnglishAlways switch to English

Math.log()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

Math.log() 函数返回一个数的自然对数,即:

x>0,Math.log(x)=ln(x)=the uniqueysuch thatey=x\forall x > 0, \mathtt{\operatorname{Math.log}(x)} = \ln(x) = \text{the unique} ; y ; \text{such that} ; e^y = x

语法

Math.log(x)

参数

x

一个数字。

描述

如果指定的number 为负数,则返回值为NaN

由于logMath 的静态方法,所以应该像这样使用:Math.log(),而不是作为你创建的Math 对象的方法。

示例

示例 1:使用Math.log

下面的函数返回指定变量的自然对数:

js
Math.log(-1); // NaN, out of rangeMath.log(0); // -InfinityMath.log(1); // 0Math.log(10); // 2.302585092994046

示例 2:使用Math.log 时基于不同的底数

下面的函数返回以x 为底y 的对数(即 logx y):

js
function getBaseLog(x, y) {  return Math.log(y) / Math.log(x);}

如果你运行getBaseLog(10, 1000),则会返回2.9999999999999996,非常接近实际答案:3,原因是浮点数精度问题。

规范

Specification
ECMAScript® 2026 Language Specification
# sec-math.log

浏览器兼容性

参见

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp