aboutsummaryrefslogtreecommitdiff
path: root/files/es/web/javascript/reference/global_objects/math/log/index.html
blob: d243cd0501e882fc0ad322e97411829fad04c075 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
---
title: Math.log()
slug: Web/JavaScript/Referencia/Objetos_globales/Math/log
translation_of: Web/JavaScript/Reference/Global_Objects/Math/log
---
<div>{{JSRef}}</div>

<p>La función <strong><code>Math.log()</code></strong> devuelve la base neutral de un número (base {{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{el unico} \; y \; \text{tal que} \; e^y = x</annotation></semantics></math></p>

<p> </p>

<p>La función en JavaScrcrip <strong><code>Math.log()</code></strong> es equivalente a <em>ln(x)</em> en matematicas.</p>

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



<h2 id="Syntax" name="Syntax">Sintaxis</h2>

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

<h3 id="Parametetros">Parametetros</h3>

<dl>
 <dt><code>x</code></dt>
 <dd>Es un numero.</dd>
</dl>

<h3 id="Retorna_el_valor">Retorna el valor</h3>

<p> </p>

<p>La base natural (base {{jsxref("Math.E", "e")}}) del número dado. Si el número es negativo, se devuelve {{jsxref("NaN")}} </p>

<h2 id="Descripcion">Descripcion</h2>

<p>If the value of <code>x</code> is negative, the return value is always {{jsxref("NaN")}}.</p>

<p>Because <code>log()</code> is a static method of <code>Math</code>, you always use it as <code>Math.log()</code>, rather than as a method of a <code>Math</code> object you created (<code>Math</code> is not a constructor).</p>

<p>If you need the natural log of 2 or 10, use the constants {{jsxref("Math.LN2")}} or {{jsxref("Math.LN10")}} .  If you need a logarithm to base 2 or 10, use {{jsxref("Math.log2()")}} or {{jsxref("Math.log10()")}} .  If you need a logarithm to other bases, use Math.log(x) / Math.log(otherBase) as in the example below; you might want to precalculate 1 / Math.log(otherBase) .</p>

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

<h3 id="Using_Math.log()">Using <code>Math.log()</code></h3>

<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="Using_Math.log()_with_a_different_base">Using <code>Math.log()</code> with a different base</h3>

<p>The following function returns the logarithm of <code>y</code> with base <code>x</code> (ie. <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">function getBaseLog(x, y) {
  return Math.log(y) / Math.log(x);
}
</pre>

<p>If you run <code>getBaseLog(10, 1000)</code> it returns <code>2.9999999999999996</code> due to floating-point rounding, which is very close to the actual answer of 3.</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES1')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.0.</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>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-math.log', 'Math.log')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>

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

<h2 id="See_also">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>