Math.log2()
함수는 숫자를 log2(숫자)로 반환합니다.
문법
Math.log2(x)
매개변수
x
- 숫자.
반환 값
주어진 숫자는 log2(숫자)로 계산합니다. 만약 숫자가 음수라면 NaN
를 반환합니다.
설명
만약 x
의 값이 0보다 작다면(음수) 값은 항상 NaN
로 반환합니다.
log2()
는 Math
의 정적 메서드이므로 만든 Math
객체의 메서드가 아니라 항상 Math.log2()
함수를 사용해야합니다. (Math
는 생성자가 없습니다.)
이 함수는 Math.log(x) / Math.log(2)와 같습니다.
따라서 log2(e)는 Math.LOG2E
와 같습니다.
그리고 위 함수는 1 / Math.LN2
과 같습니다.
예제
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
This Polyfill emulates the Math.log2
function. Note that it returns imprecise values on some inputs (like 1 << 29), wrap into Math.round()
if working with bit masks.
Math.log2 = Math.log2 || function(x) {
return Math.log(x) * Math.LOG2E;
};
표준
표준 | 상태 | 비고 |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Math.log2' in that specification. |
Standard | Initial definition. |
ECMAScript (ECMA-262) The definition of 'Math.log2' in that specification. |
Living Standard |
브라우저 호환성
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.