| layout | default |
|---|---|
| title | Math |
| parent | JavaScript |
| nav_order | 12 |
{: .no_toc .text-beta .fw-700}
{: .no_toc .text-delta }
- TOC {:toc}
숫자에 대한 수학적 작업을 수행할 때 사용
▸ 다른 객체와 다르게 Math 객체에는 생성자(constructor)가 없음 (like new Math ())
▸ Math 객체는 정적이며 Math 객체를 생성하지 않고도 모든 메소드와 특성들을 사용할 수 있음
아래는 Math 속성으로 액세스 할 수 있는 8개의 수학 상수
syntax {: .label .mt-2}
syntax {: .label .mt-2}
숫자를 정수로 바꾸는 방법
Math.round(x), Math.ceil(x), Math.floor(x), Math.trunc(x), Math.sign()
올림
Math.ceil(4.4); // returns 5반올림
Math.round(4.5) // returns 5내림
Math.floor(4.7); // returns 4정수 부분만 반환
Math.trunc(4.7); // returns 4값이 양수이면 1, 0이면 0, 음수이면 -1
Math.sign(-4); // returns -1
Math.sign(0); // returns 0
Math.sign(4); // returns 1제곱
Math.pow(8, 2); // returns 64제곱근 square root
Math.sqrt(64); // returns 8무조건 양수로
Math.abs(-4.7); // returns 4.7사인값, radians으로 표시된 x각도의 사인(-1과 1 사이의 값)을 반환
▸ 라디안 대신도를 사용하려면도를 라디안으로 변환해야합
▸ 라디안 각도 = 각도 x PI / 180
Math.sin(90 * Math.PI / 180); // returns 1 (the sine of 90 degrees)코사인값, radians으로 표시된 x각도의 코사인(-1과 1 사이의 값)을 반환
▸ 라디안 대신도를 사용하려면도를 라디안으로 변환해야합
▸ 라디안 각도 = 각도 x PI / 180
Math.cos(0 * Math.PI / 180); // returns 1 (the cos of 0 degrees)인수 목록에서 가장 낮은 또는 높은 값
Math.min(0, 150, 30, 20, -8, -200); // returns -200
Math.max(0, 150, 30, 20, -8, -200); // returns 1500(포함)과 1(제외)사이의 난수를 반환
▸ 항상 1보다 작은 값을 반환함
Math.random(); // returns a random number like 0.09714565914195883x의 자연 로그를 반환
▸ Math.E 와 Math.log()는 같음
Math.log(1); // returns 0-
0부터 9까지 랜덤한 숫자 생성
Math.floor(Math.random() * 10);
-
0부터 10까지 랜덤한 숫자 생성
Math.floor(Math.random() * 11); or Math.floor(Math.random() * 10)+1;
최소값, 최대값을 매개변수로 받는 랜덤 정수 생성함수
-
최대값 미포함
[W3School](https://www.w3schools.com/js/tryit.asp?filename=tryjs_random_function){: .btn .btn-outline .mt-2 }function getRndInteger(min, max) { return Math.floor(Math.random() * (max - min) ) + min; }
-
최대값 포함
[W3School](https://www.w3schools.com/js/tryit.asp?filename=tryjs_random_function2){: .btn .btn-outline .mt-2 }function getRndInteger(min, max) { return Math.floor(Math.random() * (max - min + 1) ) + min; }
Math 객체에 대한 모든 메소드를 볼 수 있음
[W3School](https://www.w3schools.com/jsref/jsref_obj_math.aspp){: .btn .btn-outline .mt-2 }