aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/global_objects/math/log/index.html
blob: f8d4dcf7cae7d564b249a2df26ca639cc76c552f (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
92
93
94
95
96
---
title: Math.log()
slug: Web/JavaScript/Reference/Global_Objects/Math/log
tags:
  - JavaScript
  - Math
  - Method
  - Reference
translation_of: Web/JavaScript/Reference/Global_Objects/Math/log
---
<div>{{JSRef}}</div>

<p><strong><code>Math.log()</code></strong> 関数は、 ({{jsxref("Math.E", "e")}} を底とした) 数値の自然対数を返します。</p>

<p><math display="block"><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>

<p>JavaScript の <strong><code>Math.log()</code></strong> 関数は、数学の <em>ln(x)</em> と同等です。</p>

<div>{{EmbedInteractiveExample("pages/js/math-log.html")}}</div>

<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div>

<h2 id="Syntax" name="Syntax">構文</h2>

<pre class="syntaxbox notranslate">Math.log(<var>x</var>)</pre>

<h3 id="Parameters" name="Parameters">引数</h3>

<dl>
 <dt><code><var>x</var></code></dt>
 <dd>数値です。</dd>
</dl>

<h3 id="Return_value" name="Return_value">返値</h3>

<p>({{jsxref("Math.E", "e")}} を底とした) 与えられた数値の自然対数です。数値が負の数であった場合、 {{jsxref("NaN")}} が返されます。</p>

<h2 id="Description" name="Description">解説</h2>

<p><code><var>x</var></code> の値が 0 であった場合、返値は常に {{jsxref("Number.NEGATIVE_INFINITY", "-Infinity")}} です。</p>

<p><code><var>x</var></code> の値が 0 未満であった場合、返値は常に {{jsxref("NaN")}} です。</p>

<p><code>log()</code><code>Math</code> の静的メソッドであるため、生成した <code>Math</code> オブジェクトのメソッドとしてではなく、常に <code>Math.log()</code> として使用するようにしてください (<code>Math</code> はコンストラクターではありません)。</p>

<p>2 または 10 の自然対数が必要な場合は、定数の {{jsxref("Math.LN2")}} または {{jsxref("Math.LN10")}} を使用してください。 2 や 10 を底とした対数が必要な場合は、 {{jsxref("Math.log2()")}} または {{jsxref("Math.log10()")}} を使用してください。他の数を底とした対数が必要な場合は、下記の例にあるように Math.log(x) / Math.log(otherBase) を使用してください。事前に 1 / Math.log(otherBase) を計算しておいた方がいいかもしれません。</p>

<h2 id="Examples" name="Examples"></h2>

<h3 id="Using_Math.log" name="Using_Math.log">Math.log() の使用</h3>

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

<h3 id="Using_Math.log_with_a_different_base" name="Using_Math.log_with_a_different_base">様々な底による Math.log() の使用</h3>

<p>以下の関数は、 <code>x</code> を底とした <code>y</code> の対数を返します (すなわち <math><semantics><mrow><msub><mo>log</mo><mi>x</mi></msub><mi>y</mi></mrow><annotation encoding="TeX">\log_x y</annotation></semantics></math>)。</p>

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

<p><code>getBaseLog(10, 1000)</code> を実行すると、実際の答えが 3 であるのに対し、浮動小数点の丸め処理により近似値の <code>2.9999999999999996</code> を返します。</p>

<h2 id="Specifications" name="Specifications">仕様書</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">仕様書</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-math.log', 'Math.log')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>

<p>{{Compat("javascript.builtins.Math.log")}}</p>

<h2 id="See_also" name="See_also">関連情報</h2>

<ul>
 <li>{{jsxref("Math.exp()")}}</li>
 <li>{{jsxref("Math.log1p()")}}</li>
 <li>{{jsxref("Math.log10()")}}</li>
 <li>{{jsxref("Math.log2()")}}</li>
 <li>{{jsxref("Math.pow()")}}</li>
</ul>