aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/math/log/index.html
blob: 88110bc541cfb58221b1820871fb551d6e3e4752 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
---
title: Math.log()
slug: Web/JavaScript/Reference/Global_Objects/Math/log
translation_of: Web/JavaScript/Reference/Global_Objects/Math/log
---
<p>{{JSRef("Global_Objects", "Math")}}</p>

<h2>概述</h2>

<p><code><strong>Math.log()</strong></code> 函数返回一个数的自然对数,即:</p>

<p><math><semantics><mrow><mo></mo><mi>x</mi><mo>&gt;</mo><mn>0</mn><mo>,</mo><mstyle mathvariant="monospace"><mrow><mo lspace="0em" rspace="thinmathspace">Math.log</mo><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo></mrow></mstyle><mo>=</mo><mo lspace="0em" rspace="0em">ln</mo><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo><mo>=</mo><mtext>the unique</mtext><mspace width="thickmathspace"></mspace><mi>y</mi><mspace width="thickmathspace"></mspace><mtext>such that</mtext><mspace width="thickmathspace"></mspace><msup><mi>e</mi><mi>y</mi></msup><mo>=</mo><mi>x</mi></mrow><annotation encoding="TeX">\forall x &gt; 0, \mathtt{\operatorname{Math.log}(x)} = \ln(x) = \text{the unique} \; y \; \text{such that} \; e^y = x</annotation></semantics></math></p>

<h2 id="Syntax">语法</h2>

<pre class="syntaxbox"><code>Math.log(<em>x</em>)</code></pre>

<h3 id="Parameters">参数</h3>

<dl>
 <dt><code>x</code></dt>
 <dd>一个数字.</dd>
</dl>

<h2 id="Description">描述</h2>

<p>如果指定的 <code>number</code> 为负数,则返回值为 <code>NaN</code></p>

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

<h2 id="示例">示例</h2>

<h3 id="Example:_Using_Math.log">例子1:使用<code>Math.log</code></h3>

<p>下面的函数返回指定变量的自然对数:</p>

<pre class="brush:js">Math.log(-1); // NaN, out of range
Math.log(0); // -Infinity
Math.log(1); // 0
Math.log(10); // 2.302585092994046</pre>

<h3 id="例子2_使用Math.log时基于不同的底数">例子2: 使用<code>Math.log</code>时基于不同的底数</h3>

<p>下面的函数返回以 <code>x</code> 为底 <code>y</code> 的对数(即log<sub>x</sub> y):</p>

<pre class="brush:js">function getBaseLog(x, y) {
    return Math.log(y) / Math.log(x);
}</pre>

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

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范版本</th>
   <th scope="col">规范状态</th>
   <th scope="col">注解</th>
  </tr>
  <tr>
   <td>ECMAScript 1st Edition. Implemented in JavaScript 1.0</td>
   <td>Standard</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.8.2.10', 'Math.log')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-math.log', 'Math.log')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

{{Compat}}

<h2 id="See_also">相关链接</h2>

<p></p>

<ul>
 <li>The {{jsxref("Global_Objects/Math", "Math")}} object it belongs to.</li>
 <li>{{jsxref("Math.exp()")}}</li>
 <li>{{jsxref("Math.pow()")}}</li>
</ul>