This page was translated from English by the community.Learn more and join the MDN Web Docs community.
Math.log2()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015년 7월.
Math.log2() 함수는 숫자를 log2(숫자)로 반환합니다.
In this article
시도해 보기
console.log(Math.log2(3));// Expected output: 1.584962500721156console.log(Math.log2(2));// Expected output: 1console.log(Math.log2(1));// Expected output: 0console.log(Math.log2(0));// Expected output: -Infinity문법
js
Math.log2(x);매개변수
x숫자.
반환 값
주어진 숫자를 진수로, 2를 밑으로 하는 로그 계산 결과입니다. 만약 숫자가음수라면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함수를 모방합니다. 일부 입력(예: 1 <<29)에 대해 부정확한 값을 반환할 수 있습니다. 만약 비트마스크로 작업할 경우Math.round() 로 감싸주어야 합니다.
js
if (!Math.log2) Math.log2 = function (x) { return Math.log(x) * Math.LOG2E; };예제
>Math.log2()
js
Math.log2(3); // 1.584962500721156Math.log2(2); // 1Math.log2(1); // 0Math.log2(0); // -InfinityMath.log2(-2); // NaNMath.log2(1024); // 10명세
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-math.log2> |
브라우저 호환성
같이 보기
Math.log2의 폴리 필은core-js에 존재합니다.Math.exp()Math.log()Math.log10()Math.log1p()Math.pow()