JS Math 객체를 사용하면 숫자에 대한 수학 작업을 수행할 수 있다.
다른 객체와 달리 Math 객체에는 생성자가 없다.
Math 개체는 정적이다.
Math 개체를 먼저 생성하지 않고도 모든 메서드와 속성을 사용할 수 있다.
모든 Math 속성 구문은 Math.property
다.
JavaScript는 Math 속성으로 액세스할 수 있는 8개의 수학 상수
를 제공한다.
모든 Math 메서드 구문은 다음과 같다. Math.method(number)
숫자를 정수로 반올림하는 4가지 일반적인 방법 :
Math.round(x) | Returns x rounded to its nearest integer |
Math.ceil(x) | Returns x rounded up to its nearest integer |
Math.floor(x) | Returns x rounded down to its nearest integer |
Math.trunc(x) | Returns the integer part of x (new in ES6) |
Math.round(x)
는 가장 가까운 정수를 반환한다.
Math.ceil(x)
는 가장 가까운 정수로 반올림된 x 값을 반환한다.
Math.floor(x)
는 가장 가까운 정수로 내림한 x 값을 반환한다.
Math.trunc(x)
는 x의 정수 부분을 반한다.
Math.sign(x)
는 x
가 음수
, null
또는 양수
이면 반환한다.
Math.trunc()
및Math.sign()
은 JavaScript 2015 - ES6에 추가되었다.Math.pow()
Math.pow(x, y)
는x
의 값을y
의 거듭제곱으로 반환한다.
Math.sqrt(x)
는 x
의 제곱근을 반환한다.
Math.abs(x)
는 x
의 절대값을 반환한다.
Math.sin(x)
는 각도 x
(라디안으로 지정)의 sin 값을 반환한다.
라디안
대신 각도
를 사용하려면 각도
를 라디안
으로 변환해야 한다.
라디안 단위 각도 = 각도 x PI / 180
Math.cos(x)
는 각도 x
(라디안으로 지정)의 cos 값을 반환한다.
라디안
대신 각도
를 사용하려면 각도
를 라디안
으로 변환해야 한다.
라디안 단위 각도 = 각도 x PI / 180
Math.min()
및 Math.max()
는 인수 목록에서 가장 낮거나 높은 값을 찾는 데 사용할 수 있다.
Math.random()
은 0≤ x <1 사이의 난수를 반환한다.
이 튜토리얼의 다음 장에서
Math.random()
에 대해 자세히 알아보자.
JS의 Math.log()
함수는 ln(x)
와 수학적으로 같다.
밑이 e
인 log함수를 자연로그라 한다.
Math.E
와 Math.log()
는 동일하다.
Math.log2(x)
는 x
의 밑이 2인 로그를 반환한다.
Math.log10(x)
는 x
의 밑이 10인 로그를 반환한다.
Method | Description |
---|---|
abs(x) | Returns the absolute value of x |
acos(x) | Returns the arccosine of x, in radians |
acosh(x) | Returns the hyperbolic arccosine of x |
asin(x) | Returns the arcsine of x, in radians |
asinh(x) | Returns the hyperbolic arcsine of x |
atan(x) | Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians |
atan2(y, x) | Returns the arctangent of the quotient of its arguments |
atanh(x) | Returns the hyperbolic arctangent of x |
cbrt(x) | Returns the cubic root of x |
ceil(x) | Returns x, rounded upwards to the nearest integer |
cos(x) | Returns the cosine of x (x is in radians) |
cosh(x) | Returns the hyperbolic cosine of x |
exp(x) | Returns the value of Ex |
floor(x) | Returns x, rounded downwards to the nearest integer |
log(x) | Returns the natural logarithm (base E) of x |
max(x, y, z, ..., n) | Returns the number with the highest value |
min(x, y, z, ..., n) | Returns the number with the lowest value |
pow(x, y) | Returns the value of x to the power of y |
random() | Returns a random number between 0 and 1 |
round(x) | Rounds x to the nearest integer |
sign(x) | Returns if x is negative, null or positive (-1, 0, 1) |
sin(x) | Returns the sine of x (x is in radians) |
sinh(x) | Returns the hyperbolic sine of x |
sqrt(x) | Returns the square root of x |
tan(x) | Returns the tangent of an angle |
tanh(x) | Returns the hyperbolic tangent of a number |
trunc(x) | Returns the integer part of a number (x) |
Math 객체에 대한 완벽한 내용 참고를 위해 아래 주소를 참조하자
(참조 : JavaScript Math Reference)