[JavaScript] 루트와 제곱 구하는 방법

yezo cha·2021년 6월 7일
0

JavaScript

목록 보기
11/19

루트값과 제곱값 구하기

자바스크립트 내장 함수를 사용해 제곱값과 루트값을 구할 수 있다.
Math 객체의 함수이다.
Math.sqrt()Math.pow()를 사용해보자.

루트값 구하기. Math.sqrt()

함수 인자로 값을 넘겨주면 루트 값을 반환한다.
만약 매개변수가 음수이면 NaN을 반환한다.

Math.sqrt(4);		// 2
Math.sqrt(16);		// 4
Math.sqrt(100);		// 10
Math.sqrt(2); 		// 1.414213562373095
Math.sqrt(-1); 		// NaN

제곱값 구하기. Math.pow()

Math.pow(base, exponent)
  • base : The base number.
  • exponent : The exponent used to raise the base.
// simple
Math.pow(7, 2);    	// 49
Math.pow(7, 3);    	// 343
Math.pow(2, 10);   	// 1024
// fractional exponents
Math.pow(4, 0.5);  	// 2 (square root of 4)
Math.pow(8, 1/3);  	// 2 (cube root of 8)
// signed exponents
Math.pow(7, -2);   	// 0.02040816326530612 (1/49)
Math.pow(8, -1/3); 	// 0.5
// signed bases
Math.pow(-7, 2);   	// 49 (squares are positive)
Math.pow(-7, 3);   	// -343 (cubes can be negative)
Math.pow(-7, 1/3); 	// NaN
profile
(ง •̀_•́)ง

0개의 댓글