JavaScript Math log2 ()

Die JavaScript-Funktion Math.log2 () gibt den Basis-2-Logarithmus einer Zahl zurück.

Dies entspricht log 2 (x) in der Mathematik.

Die Syntax der Math.log2()Funktion lautet:

 Math.log2(x)

log2()Da es sich um eine statische Methode handelt, wird sie mit dem MathKlassennamen aufgerufen .

Math.log2 () Parameter

Die Math.log2()Funktion umfasst:

  • x - Eine Zahl

Rückgabewert von Math.log2 ()

  • Gibt den Logarithmus zur Basis 2 der angegebenen Zahl zurück.
  • Rückgabe NaNfür negative Zahlen und nicht numerische Argumente.

Beispiel: Verwenden von Math.log2 ()

 // Using Math.log2() var value = Math.log2(1); console.log(value); // 0 var value = Math.log2(2); console.log(value); // 1 var value = Math.log2("10"); console.log(value); // 3.321928094887362 var value = Math.log2(0); console.log(value); // -Infinity var value = Math.log2(-1); console.log(value); // NaN

Ausgabe

 0 1 3.321928094887362 -Infinity NaN

Anmerkungen:

  • Verwenden Sie die Konstante Math.LOG2Efür log 2 (e) .
  • Verwenden Sie die Funktionen Math.log()oder Math.log10()für die Logarithmusbasis e und 10 .

Empfohlene Lektüre:

  • Math.log ()
  • Math.log10 ()

Interessante Beiträge...