๐Ÿ“– Math

c_yjยท2022๋…„ 7์›” 5์ผ
0

DeepDive

๋ชฉ๋ก ๋ณด๊ธฐ
26/42
post-thumbnail

ํ‘œ์ค€ ๋นŒํŠธ์ธ ๊ฐ์ฒด์ธ Math๋Š” ์ˆ˜ํ•™์ ์ธ ์ƒ์ˆ˜์™€ ํ•จ์ˆ˜๋ฅผ ์œ„ํ•œ ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณต. Math๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Math๋Š” ์ •์  ํ”„๋กœํผํ‹ฐ์™€ ์ •์  ๋ฉ”์„œ๋“œ๋งŒ ์ œ๊ณต

โœ”๏ธ Math ํ”„๋กœํผํ‹ฐ

โœ๏ธ Math.PI

์›์ฃผ์œจ PI๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

Math.PI; // 3.141592653589793

โœ”๏ธ Math ๋ฉ”์„œ๋“œ

โœ๏ธ Math.abs

Math.abs ๋ฉ”์„œ๋“œ๋Š” ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋œ ์ˆซ์žก์ด ์ ˆ๋Œ€๊ฐ’์„ ๋ฐ˜ํ™˜. ์ ˆ๋Œ€๊ฐ’์€ ๋ฐ˜๋“œ์‹œ 0 ๋˜๋Š” ์–‘์ˆ˜์ด์–ด์•ผ ํ•œ๋‹ค.

Math.abs(-1); // 1
Math.abs('-1'); // 1
Math.abs(''); // 0
Math.abs([]); // 0
Math.abs(null); // 0
Math.abs(undefined); // NaN
Math.abs({}); // NaN
Math.abs('string'); // NaN
Math.abs(); // NaN

โœ๏ธ Math.round

Math.round ๋ฉ”์„œ๋“œ๋Š” ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋œ ์ˆซ์ž์˜ ์†Œ์ˆ˜์  ์ดํ•˜๋ฅผ ๋ฐ˜์˜ฌ๋ฆผํ•œ ์ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜

Math.round(1.4); // 1
Math.round(1.6); // 2
Math.round(-1.4); // -1
Math.round(-1.6); // -2
Math.round(1); // 1
Math.round(); // NaN

โœ๏ธ Math.ceil

Math.ceil ๋ฉ”์„œ๋“œ๋Š” ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋œ ์ˆซ์ž์˜ ์†Œ์ˆ˜์  ์ดํ•˜๋ฅผ ์˜ฌ๋ฆผํ•œ ์ •์ˆ˜๋ฅผ ๋ฐ˜ํ™˜

Math.ceil(1.4); // 2
Math.ceil(1.6); // 2
Math.ceil(-1.4); // -1
Math.ceil(-1.6); // -1
Math.ceil(1); // 1
Math.ceil(); // NaN

โœ๏ธ Math.floor

Math.floor ๋ฉ”์„œ๋“œ๋Š” ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋œ ์ˆซ์ž์˜ ์†Œ์ˆ˜์  ์ดํ•˜๋ฅผ ๋‚ด๋ฆผํ•œ ์ •์ˆ˜๋ฅผ ๋ฐ˜ํ™˜

Math.floor(1.9); // 1
Math.floor(9.1); // 9
Math.floor(-1.9); // -2
Math.floor(-9.1); // -10
Math.floor(1); // 1
Math.floor(); // NaN

โœ๏ธ Math.sqrt

Math.sqrt ๋ฉ”์„œ๋“œ๋Š” ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋œ ์ˆซ์ œ์˜ ์ œ๊ณฑ๊ทผ์„ ๋ฐ˜ํ™˜

Math.sqrt(9); // 3
Math.sqrt(-9); // NaN
Math.sqrt(2); // 1.41421356
Math.sqrt(1); // 1
Math.sqrt(0); // 0
Math.sqrt(); // NaN

โœ๏ธ Math.random โญ๏ธ

Math.random ๋ฉ”์„œ๋“œ๋Š” ์ž„์˜์˜ ๋‚œ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

Math.random(); // 0์—์„œ 1 ๋ฏธ๋งŒ์˜ ๋žœ๋ค ์‹ค์ˆ˜

const random = Math.floor((Math.random() * 10) + 1);
console.log(random); // 1์—์„œ 10 ๋ฒ”์œ„์˜ ์ •์ˆ˜

โœ๏ธ Math.pow

Math.pow ๋ฉ”์„œ๋“œ๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋ฅผ ๋ฐ‘์œผ๋กœ, ๋‘ ๋ฒˆ์จฐ ์ธ์ˆ˜๋ฅผ ์ง€์ˆ˜๋กœ ๊ฑฐ๋“ญ์ œ๊ณฑํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜

Math.pow(2,8); // 256
Math.pow(2, -1); // 0.5
Math.pow(2); // NaN

Math.pow ๋ฉ”์„œ๋“œ ๋Œ€์‹  ES7์—์„œ ๋„์ž…๋œ ์ง€์ˆ˜ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ€๋…์„ฑ์ด ๋” ์ข‹์•„์ง„๋‹ค

2 ** 2 ** 2; // 16
Math.pow(Math.pow(2,2),2); // 16

โœ๏ธ Math.max

Math.max ๋ฉ”์„œ๋“œ๋Š” ์ „๋‹ฌ๋ฐ›์€ ์ธ์ˆ˜ ์ค‘์—์„œ ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜. ์ธ์ˆ˜๊ฐ€ ์ „๋‹ฌ๋˜์ง€ ์•Š์œผ๋ฉด -Infinity๋ฅผ ๋ฐ˜ํ™˜

Math.max(1); // 1
Math.max(1,2); // 2
Math.max(1,2,3); // 3
Math.max(); // -Infinity

โœ๏ธ Math.min

Math.min ๋ฉ”์„œ๋“œ๋Š” ์ „๋‹ฌ๋ฐ›์€ ์ธ์ˆ˜ ์ค‘์—์„œ ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜. ์ธ์ˆ˜๊ฐ€ ์ „๋‹ฌ๋˜์ง€ ์•Š์œผ๋ฉด Infinity๋ฅผ ๋ฐ˜ํ™˜

Math.min(1); // 1
Math.min(1,2); // 1
Math.min(1,2,3); // 1
Math.min(); // Infinity
profile
FrontEnd Developer

0๊ฐœ์˜ ๋Œ“๊ธ€