Skip to content

JavaScript์˜ BigInt ์ž๋ฃŒํ˜•ย #8

@dididy

Description

@dididy

์กฐํ•ฉ ๋ฌธ์ œ๋ฅผ ํ’€๋‹ค๊ฐ€ 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) // true

BigInt๋Š” Number์™€ ์ผ์น˜ํ•˜์ง€ ์•Š์ง€๋งŒ ๋™๋“ฑํ•˜๋‹ค. ๋˜ํ•œ ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•(๋น„๊ต์—ฐ์‚ฐ์ž ์‚ฌ์šฉ)์œผ๋กœ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋‹ค.

๋˜ํ•œ ๋ฐฐ์—ด์˜ ์š”์†Œ๋กœ ํ˜ผ์šฉ๋˜์–ด ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ์—๋„ ์ •๋ ฌ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

Reference

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/BigInt

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions