Math.log()
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.log()
์ ์ ๋ฉ์๋๋ ์ซ์์ ์์ฐ ๋ก๊ทธ(๋ฐ์ด e)๋ฅผ ๋ฐํํฉ๋๋ค. ์ฆ, ์๋์ ๊ฐ์ต๋๋ค.
์๋ํด ๋ณด๊ธฐ
function getBaseLog(x, y) {
return Math.log(y) / Math.log(x);
}
// 2 x 2 x 2 = 8
console.log(getBaseLog(2, 8));
// Expected output: 3
// 5 x 5 x 5 x 5 = 625
console.log(getBaseLog(5, 625));
// Expected output: 4
๊ตฌ๋ฌธ
Math.log(x)
๋งค๊ฐ๋ณ์
x
-
0 ์ด์์ ์
๋ฐํ ๊ฐ
x
์ ์์ฐ ๋ก๊ทธ(๋ฐ์ด e). ๋ง์ฝ x
๊ฐ ยฑ0์ผ ๊ฒฝ์ฐ -Infinity
์ ๋ฐํํ๋ฉฐ, x < 0
์ด๋ผ๋ฉด NaN
์ ๋ฐํํฉ๋๋ค.
์ค๋ช
log()
๋ Math
์ ์ ์ ๋ฉ์๋์ด๊ธฐ ๋๋ฌธ์, ํญ์ Math.log()
๋ก ์ฌ์ฉํฉ๋๋ค. ์์ฑํ Math
๊ฐ์ฒด์ ๋ฉ์๋๋ก ์ฌ์ฉํ์ง ์์ต๋๋ค (Math
๋ ์์ฑ์๊ฐ ์๋๋๋ค).
2๋ 10์ ์์ฐ ๋ก๊ทธ๊ฐ ํ์ํ๋ค๋ฉด, Math.LN2
๋ Math.LN10
์์๋ฅผ ์ฌ์ฉํ์ธ์. ๋ฐ์ด 2๋ 10์ธ ๋ก๊ทธ๊ฐ ํ์ํ๋ค๋ฉด, Math.log2()
ํน์ Math.log10()
์ ์ฌ์ฉํ์ธ์. ๋ค๋ฅธ ๋ฐ์ ๋ก๊ทธ๊ฐ ํ์ํ๋ค๋ฉด, ์๋ ์์์ฒ๋ผ Math.log(x) / Math.log(otherBase)
๋ฅผ ์ฌ์ฉํ์ธ์. ์ด ๊ฒฝ์ฐ 1 / Math.log(otherBase)
๋ฅผ ๋ฏธ๋ฆฌ ๊ณ์ฐํด ๋๋ ๊ฒ์ด ์ข์ต๋๋ค. Math.log(x) * constant
๊ณฑ์
์ด ํจ์ฌ ๋ ๋น ๋ฅด๊ธฐ ๋๋ฌธ์
๋๋ค.
1์ ๋งค์ฐ ๊ฐ๊น์ด ์์์ ๊ฒฝ์ฐ ์ ๋ฐ๋ ์์ค์ด ๋ฐ์ํ ์ ์์ด ์์ฐ ๋ก๊ทธ์ ์ ํ๋๊ฐ ๋จ์ด์ง ์ ์์ต๋๋ค. ์ด๋ฐ ๊ฒฝ์ฐ์๋ Math.log1p
๋ฅผ ๋์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
์์
Math.log() ์ฌ์ฉํ๊ธฐ
๋ค์ ํจ์๋ ๋ฐ x
์ y
์ ๋ก๊ทธ๋ฅผ ๋ฐํํฉ๋๋ค. (์: )
function getBaseLog(x, y) {
return Math.log(y) / Math.log(x);
}
getBaseLog(10, 1000)
์ ์คํํ๋ฉด 2.9999999999999996
์ ๋ฐํํฉ๋๋ค. ์ด๋ ๋ถ๋ ์์์ ๋ฐ์ฌ๋ฆผ๋๋ฌธ์ธ๋ฐ, ์ค์ ๋ต์ธ 3๊ณผ ๋งค์ฐ ๊ทผ์ ํฉ๋๋ค.
๋ช ์ธ์
Specification |
---|
ECMAScriptยฎ 2026 Language Specification # sec-math.log |
๋ธ๋ผ์ฐ์ ํธํ์ฑ
Loadingโฆ