A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math/log below:

Math.log() - JavaScript | MDN

Math.log()

Baseline Widely available

概述

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

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

语法 参数 描述

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

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

示例 示例 1:使用Math.log

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

Math.log(-1); // NaN, out of range
Math.log(0); // -Infinity
Math.log(1); // 0
Math.log(10); // 2.302585092994046
示例 2:使用 Math.log 时基于不同的底数

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

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

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

规范 浏览器兼容性 参见

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4