-
Notifications
You must be signed in to change notification settings - Fork 0
Description
์กฐํฉ ๋ฌธ์ ๋ฅผ ํ๋ค๊ฐ BigInt๋ฅผ ์ฌ์ฉํด์ผ ํ๋ ์ํฉ์ด ์๊ฒผ๋ค. BigInt์ ๊ฐ๋ ์ ์ ๋ฆฌํด๋ณด์.
ํต์ฌ ๊ฐ๋
JavaScript์ ์์ํ ์ค ํ๋์ด๋ค. Number๋ก ํํํ ์ ์๋ ๋ฒ์์ธ 2^53 - 1 ๋ณด๋ค ํฐ ์ ์๋ฅผ ํํํ ๋ ์ฌ์ฉํ๋ค.
typeof 1n === 'bigint'; // true
typeof BigInt('1') === 'bigint'; // true์ฌ์ฉ๋ฒ์ ๊ฐ๋จํ๋ค. BigInt() ํจ์๋ฅผ ํธ์ถํ๊ฑฐ๋ ์ซ์ ๋ค์ n์ ๋ถ์ด๋ฉด ๋๋ค. BigInt๋ typeof์ 'bigint'๋ก ๊ฒ์ฆํ ์ ์๋ค.
์ฐ์ฐ์๋ +, *, - **, % ๋ฅผ ์ฌ์ฉํ ์ ์์ผ๋ >>>(๋ถํธ ๋ฒ๋ฆผ ์ค๋ฅธ์ชฝ ์ํํธ)๋ ์ฌ์ฉํ ์ ์๋ค. ๋จํญ + ์ฐ์ฐ์๋ ์ง์ํ์ง ์๋๋ค.
๋๋๊ธฐ ์ฐ์ฐ์ ํ ๋ ์์์ ์ ๋ฒ๋ฆฐ๋ค. ๊ทธ ์ด์ ๋ BigInt๋ BitDecimal์ด ์๋๊ธฐ ๋๋ฌธ์ด๋ค. ์ด ์ ์ ์ฃผ์ํด์ผ ํ๋ค.
console.log(0n === 0) // false
console.log(0n == 0) // trueBigInt๋ Number์ ์ผ์นํ์ง ์์ง๋ง ๋๋ฑํ๋ค. ๋ํ ์ผ๋ฐ์ ์ธ ๋ฐฉ๋ฒ(๋น๊ต์ฐ์ฐ์ ์ฌ์ฉ)์ผ๋ก ๋น๊ตํ ์ ์๋ค.
๋ํ ๋ฐฐ์ด์ ์์๋ก ํผ์ฉ๋์ด ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ์๋ ์ ๋ ฌ์ด ๊ฐ๋ฅํ๋ค.
Reference
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/BigInt