La funció Math.log2()
retorna el logaritme en base 2 d'un nombre, és a dir
Sintaxi
Math.log2(x)
Paràmetres
x
- Un nombre.
Descripció
Si el valor de x
és menor que 0, el valor retornat sempre serà NaN
.
Com que log2()
és un mètode estàtic de Math
, sempre s'utilitza com Math.log2()
en comptes de com un mètode d'un objecte Math
creat (Math
no és un constructor).
Exemples
Utilitzar Math.log2()
Math.log2(3); // 1.584962500721156
Math.log2(2); // 1
Math.log2(1); // 0
Math.log2(0); // -Infinit
Math.log2(-2); // NaN
Math.log2(1024); // 10
Polyfill
Aquest Polyfill emula la funció Math.log2
. Cal destacar que retorna valors imprecisos per a algunes entrades (com ara 1 << 29), embolcalleu-la amb Math.round()
si esteu treballant amb màscares de bits.
Math.log2 = Math.log2 || function(x) {
return Math.log(x) / Math.LN2;
};
Especificacions
Especificació | Estat | Comentaris |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Math.log2' in that specification. |
Standard | Definició inicial. |
Compatibilitat amb navegadors
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Característica | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Suport bàsic | 38 | 25 (25) | No support | 25 | 7.1 |
Característica | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Suport bàsic | No support | No support | 25.0 (25) | No support | No support | 8 |