aboutsummaryrefslogtreecommitdiff
path: root/files/de/web/javascript/reference/global_objects/math/log2/index.html
blob: 988ae2c06c50f86bdb5c5ffbede5265b5d07883c (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
---
title: Math.log2()
slug: Web/JavaScript/Reference/Global_Objects/Math/log2
tags:
  - ECMAScript 2015
  - JavaScript
  - Math
  - Method
  - Reference
translation_of: Web/JavaScript/Reference/Global_Objects/Math/log2
---
<div>{{JSRef}}</div>

<p>Die <strong><code>Math.log2()</code></strong> Funktion gibt den Logarithmus zur Basis<code> 2</code> zurück. Das bedeutet</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.log2</mo><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo></mrow></mstyle><mo>=</mo><msub><mo lspace="0em" rspace="0em">log</mo><mn>2</mn></msub><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo><mo>=</mo><mtext></mtext><mspace width="thickmathspace"></mspace><mi>y</mi><mspace width="thickmathspace"></mspace><mtext>so das</mtext><mspace width="thickmathspace"></mspace><msup><mn>2</mn><mi>y</mi></msup><mo>=</mo><mi>x</mi></mrow><annotation encoding="TeX">\forall x &gt; 0, \mathtt{\operatorname{Math.log2}(x)} = \log_2(x) = \text{the unique} \; y \; \text{such that} \; 2^y = x</annotation></semantics></math></p>

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



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

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

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

<dl>
 <dt><code>x</code></dt>
 <dd>Eine Zahl.</dd>
</dl>

<h3 id="Rückgabewert">Rückgabewert</h3>

<p>Den Logarithmus zur Basis 2 der gegebenen Zahl. Wenn die Zahl negativ ist, wird {{jsxref("NaN")}} zurückgegeben.</p>

<h2 id="Beschreibung">Beschreibung</h2>

<p>Wenn der Wert von <code>x</code> negativ ist, so gibt die Funktion immer <a href="https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/NaN" title="Die globale NaN Eigenschaft ist ein Wert, der Not-A-Number (keine Zahl) repräsentiert."><code>NaN</code></a> zurück.</p>

<p>Weil <code>log2()</code> eine statische Funktion von <code>Math</code> ist, wird es immer als <code>Math.log2()</code> eingesetzt<code>,</code> jedoch nicht als Methode eines erzeugten <code>Math</code> Objektes (<code>Math</code> ist kein Konstruktor).</p>

<p>Diese Funktion ist äquivalent zu Math.log(x) / Math.log(2). Für log2(e) gibt es die Konstante {{jsxref("Math.LOG2E")}} welche 1 / {{jsxref("Math.LN2")}} ist.</p>

<h2 id="Beispiele">Beispiele</h2>

<h3 id="Einsatz_von_Math.log2()">Einsatz von <code>Math.log2()</code></h3>

<pre class="brush: js">Math.log2(3);    // 1.584962500721156
Math.log2(2);    // 1
Math.log2(1);    // 0
Math.log2(0);    // -Infinity
Math.log2(-2);   // NaN
Math.log2(1024); // 10
</pre>

<h2 id="Polyfill">Polyfill</h2>

<p>Diese Funktion kann mit folgender Funktion emitiert werden. Es kann sein, dass diese Funktion bei einigen eingabewerten ungenau (ungefähr 1 &lt;&lt; 29) ist. Schachtel die Funktion in {{jsxref("Math.round()")}} wenn mit Bitmasken gearbeitet wird.</p>

<pre class="brush: js line-numbers  language-js"><code class="language-js">Math<span class="punctuation token">.</span>log2 <span class="operator token">=</span> Math<span class="punctuation token">.</span>log2 <span class="operator token">||</span> <span class="keyword token">function</span><span class="punctuation token">(</span>x<span class="punctuation token">)</span> <span class="punctuation token">{</span>
  <span class="keyword token">return</span> Math<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>x<span class="punctuation token">)</span> <span class="operator token">*</span> Math<span class="punctuation token">.</span>LOG2E<span class="punctuation token">;</span>
<span class="punctuation token">}</span><span class="punctuation token">;</span></code></pre>

<h2 id="Spezifikationen">Spezifikationen</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Spezifikation</th>
   <th scope="col">Status</th>
   <th scope="col">Kommentar</th>
  </tr>
  <tr>
   <td>{{SpecName('ES2015', '#sec-math.log2', 'Math.log2')}}</td>
   <td>{{Spec2('ES2015')}}</td>
   <td>Initiale Definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-math.log2', 'Math.log2')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Browserkompatibilität">Browserkompatibilität</h2>

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

<h2 id="Siehe_auch">Siehe auch</h2>

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