본문 바로가기
study/Java

[Java] 30. 기본 API (Math)

by 금이패런츠 2022. 3. 28.
728x90
반응형
package chap11;
/*
 * Math 클래스 : 수치계산관련 클래스
 *   1. final 클래스
 *   2. 생성자의 접근제어자가 private임. => 객체 생성 불가.
 *      모든 멤버가 클래스 멤버임.
 *      상수 : PI : 원주율
 *            E  : 자연로그
 */
public class MathEx1 {
	public static void main(String[] args) {
		//abs : 절대값(양수)
		System.out.println("Math.abs(5)=" + Math.abs(5)); //5
		System.out.println("Math.abs(-5)=" + Math.abs(-5)); //5
		System.out.println("Math.abs(3.14)=" + Math.abs(3.14)); //3.14
		System.out.println("Math.abs(-3.14)=" + Math.abs(-3.14)); //3.14
		
		// : 근사정수 : 가까운 정수
		//ceil : 큰 수 중 가까운 정수
		System.out.println("Math.ceil(5.4)=" + Math.ceil(5.4)); //6.0
		System.out.println("Math.ceil(-5.4)=" + Math.ceil(-5.4)); //-5.0
		//floor : 작은 수 중 가까운 정수
		System.out.println("Math.floor(5.4)=" + Math.floor(5.4)); //5.0
		System.out.println("Math.floor(-5.4)=" + Math.floor(-5.4)); //-6.0
		//rint : 가까운 정수
		System.out.println("Math.rint(5.4)=" + Math.rint(5.4)); //5.0
		System.out.println("Math.rint(-5.4)=" + Math.rint(-5.4)); //-5.0
		
		//최소값, 최대값
		System.out.println("Math.min(5,4)=" + Math.min(5,4)); //4
		System.out.println("Math.min(5.4,5.3)=" + Math.min(5.4,5.3)); //5.3
		System.out.println("Math.max(5,4)=" + Math.max(5,4)); //5
		System.out.println("Math.max(5.4,5.3)=" + Math.max(5.4,5.3)); //5.4
		
		//반올림 : long round() : 리턴형이 long임 (소숫점이하 없음) 
		System.out.println("Math.round(5.4)=" + Math.round(5.4)); //5
		System.out.println("Math.round(5.5)=" + Math.round(5.5)); //6
		
		//난수 : 0 <= x < 1.0
		System.out.println(Math.random());
		
		//삼각함수 : 각도 단위는 라디안임
		//Math.PI/2 라디안 = 90도
		System.out.println("Math.sin(Math.PI/2)=" + Math.sin(Math.PI/2)); //1.0
		//Math.toRadians(60) : 60도를 라디안 단위로 변경
		System.out.println("Math.cos(Math.toRadians(60)=" + Math.cos(Math.toRadians(60))); //0.5000000000001
		System.out.println("Math.tan(Math.PI/4)=" + Math.tan(Math.PI/4)); //0.99999999999999
		//toDegrees(라디안) : 라디안단위를 도단위로 변경
		System.out.println("Math.toDegrees(Math.PI/2)=" + Math.toDegrees(Math.PI/2)); //45.0
		
		//log
		System.out.println("Math.log(Math.E)=" + Math.log(Math.E)); //1.0
		
		//제곱근
		System.out.println("Math.sqrt(25)=" + Math.sqrt(25)); //5.0
		//제곱
		System.out.println("Math.pow(5,3)=" + Math.pow(5,3)); //5*5*5
	}
}
728x90
반응형