Die Math.log2()
Funktion gibt den Logarithmus zur Basis 2
zurück. Das bedeutet
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
Math.log2(x)
Parameters
x
- Eine Zahl.
Rückgabewert
Den Logarithmus zur Basis 2 der gegebenen Zahl. Wenn die Zahl negativ ist, wird NaN
zurückgegeben.
Beschreibung
Wenn der Wert von x
negativ ist, so gibt die Funktion immer NaN
zurück.
Weil log2()
eine statische Funktion von Math
ist, wird es immer als Math.log2()
eingesetzt,
jedoch nicht als Methode eines erzeugten Math
Objektes (Math
ist kein Konstruktor).
Diese Funktion ist äquivalent zu Math.log(x) / Math.log(2). Für log2(e) gibt es die Konstante Math.LOG2E
welche 1 / Math.LN2
ist.
Beispiele
Einsatz von Math.log2()
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
Polyfill
Diese Funktion kann mit folgender Funktion emitiert werden. Es kann sein, dass diese Funktion bei einigen eingabewerten ungenau (ungefähr 1 << 29) ist. Schachtel die Funktion in Math.round()
wenn mit Bitmasken gearbeitet wird.
Math.log2 = Math.log2 || function(x) {
return Math.log(x) * Math.LOG2E;
};
Spezifikationen
Spezifikation | Status | Kommentar |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) Die Definition von 'Math.log2' in dieser Spezifikation. |
Standard | Initiale Definition. |
ECMAScript (ECMA-262) Die Definition von 'Math.log2' in dieser Spezifikation. |
Lebender Standard |
Browserkompatibilität
BCD tables only load in the browser
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.