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
|
---
title: Math.imul()
slug: Web/JavaScript/Reference/Global_Objects/Math/imul
tags:
- JavaScript
- Math
- Method
- Reference
translation_of: Web/JavaScript/Reference/Global_Objects/Math/imul
---
<div>{{JSRef}}</div>
<p>Die <strong><code>Math.imul()</code></strong> Funktion führt eine C ähnliche 32-Bit Multiplikation der zwei Parameter durch.</p>
<div>{{EmbedInteractiveExample("pages/js/math-imul.html")}}</div>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox"><code>Math.imul(<var>a</var>, <var>b</var>)</code></pre>
<h3 id="Parameter">Parameter</h3>
<dl>
<dt><code>a</code></dt>
<dd>Erste Nummer.</dd>
<dt><code>b</code></dt>
<dd>Zweite Nummer.</dd>
</dl>
<h3 id="Rückgabewert">Rückgabewert</h3>
<p>Das Resultat der C-ähnlichen 32-Bit Multiplikation der übergebenen Parameter.</p>
<h2 id="Beschreibung">Beschreibung</h2>
<p><code>Math.imul()</code> erlaubt es schnelle 32-Bit Ganzzahlmultiplikationen mit C ähnlicher Semantik durchzuführen. Diese Funktion ist nützlich für Projekte wie <a href="http://en.wikipedia.org/wiki/Emscripten">Emscripten</a>. Weil <code>imul()</code> eine statische Methode von <code>Math</code> ist, muss man immer <code>Math.imul()</code> benutzen und nicht als Methode eines <code>Math</code> Objektes, das man erzeugt (<code>Math</code> ist kein Konstruktor). Wenn normale JavaScript Gleitkommazahlen in <code>imul</code> eingesetzt werden, wird die Performance beeinträchtigt. Das ist wegen der Umwandlung von Gleitkommazahlen zu ganzen Zahlen für die Multiplikation und die anschließende Rückkonvertierung des Ergebnisses in eine Gleitkommazahl. Der Grund warum <code>imul</code> existiert, ist, dass esin (nur) einem Fall schneller ist: AsmJS. AsmJS erlaubt JIST-Optimierungen für einfache Implementierung von ganzen Zahlen in JavaScript. Multiplizieren von zwei Zahlen mit <code>imul</code>, die intern als Integer dargestellt sind (was nur mit AsmJS funktioniert) ist der einzige Grund, wo <code>Math.imul</code> die Performance im Browsern steigern kann.</p>
<h2 id="Beispiele">Beispiele</h2>
<h3 id="Einsatz_von_Math.imul()">Einsatz von <code>Math.imul()</code></h3>
<pre class="brush: js">Math.imul(2, 4); // 8
Math.imul(-1, 8); // -8
Math.imul(-2, -2); // 4
Math.imul(0xffffffff, 5); // -5
Math.imul(0xfffffffe, 5); // -10
</pre>
<h2 id="Polyfill">Polyfill</h2>
<p>Diese Funktionalität kann durch folgende Funktion emuliert werden:</p>
<pre class="brush: js">Math.imul = Math.imul || function(a, b) {
var aHi = (a >>> 16) & 0xffff;
var aLo = a & 0xffff;
var bHi = (b >>> 16) & 0xffff;
var bLo = b & 0xffff;
// the shift by 0 fixes the sign on the high part
// the final |0 converts the unsigned value into a signed value
return ((aLo * bLo) + (((aHi * bLo + aLo * bHi) << 16) >>> 0) | 0);
};
</pre>
<h2 id="Spezifikationen">Spezifikationen</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('ES6', '#sec-math.imul', 'Math.imul')}}</td>
<td>{{Spec2('ES6')}}</td>
<td>Initial definition.</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-math.imul', 'Math.imul')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browserkompatibilität">Browserkompatibilität</h2>
<p>{{Compat("javascript.builtins.Math.imul")}}</p>
|