[Java/자바] Math 클래스 정리 (feat.삼각함수)

 

Math클래스는 기본적인 수학계산에 유용한 메서드로 구성되어 있습니다.

이번 포스팅을 통해 Math클래스의 자주 사용되는 메서드에 대해 다뤄볼 것이며 삼각함수 구하는 법도 다뤄보려 합니다.

 

Math 클래스

 

Math.abs()

주어진 값의 절대값을 반환합니다.

public class MathTest {
	public static void main(String[] args) {
		
		int i = Math.abs(-2);
		double d = Math.abs(-4);
		
		System.out.println(i);
		System.out.println(d);

	}
}

 

Math.sqrt() - ( double형으로 반환 )

주어진 값의 제곱근 값을 반환합니다.

public class MathTest {
	public static void main(String[] args) {
		double d = Math.sqrt(4);
		
		System.out.println(d);
		
		System.out.println(Math.sqrt(25));

	}
}

 

Math.pow() - ( double형으로 반환 )

주어진 값의 n승을 반환 합니다. ( double형으로 반환 )

 

public class MathTest {
	public static void main(String[] args) {
		double d = Math.pow(4, 2);
		
		System.out.println(d);
		System.out.println(Math.pow(11, 2));

	}
}

 

Math.ceil(), Math.floor() - ( double형으로 반환 )

ceil은 주어진 값을 올림, floor는 주어진 값을 버림하여 반환합니다.

public class MathTest {
	public static void main(String[] args) {
		double d = Math.ceil(1.4);
		
		System.out.println(d);
		System.out.println(Math.ceil(2.1));
		
		double d2 = Math.floor(1.8);
		System.out.println(d2);
		System.out.println(Math.floor(4.7));
	}
}

 

Math.round()

round는 소수점 첫쨰자리에서 반올림한 정수값을 반환합니다.

public class MathTest {
	public static void main(String[] args) {

		System.out.println(Math.round(2.5));
		System.out.println(Math.round(4.4));
		System.out.println(Math.round(2.1));
	
	}
}

 

Math.rint() - ( double형으로 반환 )

rint는 주어진 값과 가장 가까운 정수값을 double형으로 반환합니다.

단, 두 정수의 정가운데 있는 값(1.5, 2.5, 3.5 등)은 짝수를 반환

public class MathTest {
	public static void main(String[] args) {

		System.out.println(Math.rint(1.5));
		System.out.println(Math.rint(2.5));
		System.out.println(Math.rint(3.5));
		System.out.println(Math.rint(7.5));
		System.out.println(Math.rint(4.4));
		System.out.println(Math.rint(2.1));
	
	}
}

 

Math.max(), Math.min()

max는 주어진 두 값을 비교해 최대값을 반환.

min은 주어진 두 값을 비교해 최소값을 반환.

public class MathTest {
	public static void main(String[] args) {

		System.out.println(Math.max(1.2, 1.20005));
		System.out.println(Math.max(7, 1));
		System.out.println(Math.min(3.1, 3.1555));
		System.out.println(Math.min(3,11));
	}
}

 

 

Math.random() - ( double형으로 반환 )

0.0 ~ 1.0 ( 0.0 <= x < 1.0 ) 범위의 임의 double값을 반환합니다.

( 1은 범위에 포함 X )

결과는 double형으로 재밌는 수가 나오기 때문에, 정수를 얻고 싶다면 int로 형변환 해줘야합니다.

 

Math.random() * (최댓값-최소값+1) + 최소값

  • 예시) 1 ~ 12까지의 랜덤 숫자 10가지 출력→ (int) (Math.random() * 12) + 1
  • (int) (Math.random() * (12-1+1)) + 1

 

public class MathTest {
	public static void main(String[] args) {

		System.out.println((int)(Math.random() * 100)); // 0 ~ 99
		System.out.println((int)(Math.random() * 100) + 10); // 10 ~ 109
		System.out.println((int)(Math.random() * 11)); // 0 ~ 10
		System.out.println((int)(Math.random() * 33) + 1); // 1 ~ 33

	}
}

 


삼각함수 관련

sin, cos, tan 이거 다 배운지 너무 오래되서 다 까먹었을 겁니다.그래서 쉽게 외울수 있는 그림 가져오게 되었습니다.

 

삼각함수의 공식은 다음과 같습니다.

출처 :&nbsp;https://blog.naver.com/run_and_run/222843745550

 

 

위 삼각형을 이용해 각 변(a, b)의 값을 구해봅시다.

 

Math.sin() - ( double형으로 반환 )

라디언값을 받아서 사인값을 반환합니다.

import static java.lang.Math.*;

public class MathTest {
	public static void main(String[] args) {
		double c = Math.sqrt(2);
        	// double a = c * sin(PI/4); 같은 결과
		double a = c * sin(toRadians(45));
		
		System.out.println(a); 
	}
}

 

Math.cos() - ( double형으로 반환 )

라디언값을 받아서 코사인값을 반환합니다.

import static java.lang.Math.*;

public class MathTest {
	public static void main(String[] args) {
		double c = Math.sqrt(2);
		// double b = c * cos(PI/4); 같은 결과
		double b = c * cos(toRadians(45));
		
		System.out.println(b); 
	}
}

 

 

Math.tan() - ( double형으로 반환 )

라디언값을 받아서 탄젠트값을 반환합니다.

public class MathTest {
	public static void main(String[] args) {
		double t = Math.tan(Math.toRadians(45));
		double t2 = Math.tan(Math.toRadians(90));
		double t3 = Math.tan(Math.toRadians(180));

		System.out.println(t);
		System.out.println(t2);
		System.out.println(t3);

	}
}

 

Math.atan2() - ( double형으로 반환 )

직각 삼각형에서 a, b 를 알면 끼인각(각도)를 구해줍니다.

결과값은 라디안이므로 도(degree)단위로 변환하려면 toDegrees() 메서드를 이용하면 됩니다.

import static java.lang.Math.*;

public class MathTest {
	public static void main(String[] args) {
		double c = Math.sqrt(2);
		double a = c * sin(toRadians(45));  
		double b = c * cos(toRadians(45));
		
		double degree = atan2(a,b);
		
		System.out.println("라디안 :" + degree); 
		System.out.println("각도 :" + toDegrees(degree)); 
	}
}

 

Math.toDegree() - ( double형으로 반환 )

주어진 라디안 값을 각도값으로 변환합니다.

public class MathTest {
	public static void main(String[] args) {

		System.out.println(Math.toRadians(90)); // 90도를 라디언으로 변환
		
		// 90도의 라디언 값을 넣어 각도로 변환
		System.out.println(Math.toDegrees(1.5707963267948966));
	}
}

 

Math.toRadians() - ( double형으로 반환 )

주어진 각도를 "라디안"으로 변환합니다.

public class MathTest {
	public static void main(String[] args) {

		System.out.println(Math.toRadians(90)); // 90도를 라디언으로 변환
		System.out.println(Math.toRadians(45)); // 45도를 라디언으로 변환

	}
}

 

 

두 점 간의 길이 구하기

두 점의 좌표를 안다면 아래 공식을 통해 길이를 구할 수 도있습니다.

class MathEx3 {
	public static void main(String args[]) {
		int x=1, y=1;  // (1, 1)
		int x1=2, y1=2;  // (2, 2)

		double c = sqrt(pow(x1-x, 2) + pow(y1-y, 2));

		System.out.printf("c=%f%n", c);  
	}
}

 


참고자료
자바의정석3
https://velog.io/@on-n-on-turtle/java-Math.random-%EC%9B%90%ED%95%98%EB%8A%94-%EB%B2%94%EC%9C%84%EA%B9%8C%EC%A7%80-%EB%A7%8C%EB%93%9C%EB%8A%94-%EB%B2%95