Math 클래스란?
기본 지수, 로그, 제곱근 및 삼각 함수와 같은 자주 사용되는 기본 숫자 연산을 수행하기 위한 메서드를 구현한 클래스입니다. java.lang 패키지에 포함되어 별도 import를 할 필요가 없습니다. static 메서드이므로 Math.abs()와 같이 직접 호출하여 사용합니다.
https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html
Math (Java Platform SE 8 )
Returns the value of the first argument raised to the power of the second argument. Special cases: If the second argument is positive or negative zero, then the result is 1.0. If the second argument is 1.0, then the result is the same as the first argument
docs.oracle.com
주요 메서드
아래와 같이 주요 메서드를 정리해 보았습니다. 대부분 int, long, float, double을 모두 받으나, 편의상 하나만 표기했습니다.
1. Math.abs( a )
입력된 a의 절댓값을 구하는 메서드입니다.
//1. Math.abs(double a)
System.out.println("1.Math.abs()-절대값 구하기: "+Math.abs(-12));
//출력 결과
1.Math.abs()-절대값 구하기: 12
2. Math.ceil( a)
입력된 a의 소수점 첫째 자리에서 올림 한 값을 반환하는 메서드입니다.
//2. Math.ceil(double a)
System.out.println("2.Math.ceil()-소수점 첫째자리 올림: "+Math.ceil(12.3));
//출력 결과
2.Math.ceil()-소수점 첫째자리 올림: 13.0
3. Math.floor( a)
입력된 a의 소수점 첫째자리에서 버림 한 값을 반환하는 메서드입니다.
//3. Math.floor(double a)
System.out.println("3.Math.floor()-소수점 첫째자리 버림: "+Math.floor(12.6));
//출력 결과
3.Math.floor()-소수점 첫째자리 버림: 12.0
4. Math.round( a)
입력된 a의 소수점 첫째자리에서 반올림한 값을 반환하는 메서드입니다.
//4. Math.round(double a)
System.out.println("4.Math.round()-소수점 첫째자리 반올림: "+Math.round(12.3));
System.out.println("4.Math.round()-소수점 첫째자리 반올림: "+Math.round(12.6));
//출력 결과
4.Math.round()-소수점 첫째자리 반올림: 12
4.Math.round()-소수점 첫째자리 반올림: 13
※ 입력값 a를 소수점 n째 자리까지 표현하기
ceil(), floor(), round() 모두 소수점 이하를 버리기 때문에 소수점 n째 자리까지 표현하기 위해서는 별도 계산을 해주어야 합니다.
//※ 입력값 a를 소수점 n째 자리까지 표현
double a=12.34567;
int n=2;
System.out.println("※ 입력값 a를 소수점 n째 자리까지 표현: "+Math.round(a*Math.pow(10,n))/Math.pow(10,n));
//출력 결과
※ 입력값 a를 소수점 n째 자리까지 표현: 12.35
5. Math.max( a , b )
입력값 a, b중 큰 값을 반환하는 메서드입니다.
//5. Math.max(double a, double b)
System.out.println("5.Math.max()-큰 수 구하기: "+Math.max(12,34));
//출력 결과
5.Math.max()-큰 수 구하기: 34
6. Math.min( a , b )
입력값 a, b중 작은 값을 반환하는 메서드입니다.
//6. Math.min(double a, double b)
System.out.println("6.Math.min()-작은 수 구하기: "+Math.min(12,34));
//출력 결과
6.Math.min()-작은 수 구하기: 12
7. Math.pow( a , b )
입력값 a를 b제곱한 값을 반환하는 메서드입니다.
//7. Math.pow(double a, double b)
System.out.println("7.Math.pow()-제곱수 구하기): "+Math.pow(3,3));
//출력 결과
7.Math.pow()-제곱수 구하기): 27.0
8. Math.sqrt( a )
입력값 a의 제곱근을 반환하는 메서드입니다.
//8. Math.sqrt(double a)
System.out.println("8.Math.sqrt()-제곱근 구하기): "+Math.sqrt(16));
//출력 결과
8.Math.sqrt()-제곱근 구하기): 4.0
9. Math.random( )
0.0이상 1.0 미만의 임의의 실수 값을 반환하는 메서드입니다.
이때 반환하는 값의 범위를 지정한다면, 반환하고자 하는 값을 계산을 해주어야 합니다.
예를 들어 1 이상 100 이하의 자연수 중 임의의 값을 선택하고자 할 때는
Math.random() 함수가 0.0 "이상"의 값을 반환하므로 결과에 반환범위의 최솟값을 더해주어야 합니다.
그리고, Math.random()함수가 1.0 "미만"의 값을 반환하므로 반환범위의 "최댓값+1"을 곱해주어야 합니다.
+1이 없으면 반환범위의 최대값은 출력되지 않습니다.
//9. Math.random()
System.out.println("9.Math.random()-임의의 수 구하기): "+Math.random());
System.out.println("9.Math.random()-1부터 100사이의 임의의 수 구하기): "+ (int)(Math.random()*101))+1;
//출력 결과
9.Math.random()-임의의 수 구하기): 0.18719636181610533
9.Math.random()-1부터 100사이의 임의의 수 구하기): 85
전체 코드
public class MathTest {
public static void main(String[] args){
//1. Math.abs(double a)
System.out.println("1.Math.abs()-절대값 구하기: "+Math.abs(-12));
//2. Math.ceil(double a)
System.out.println("2.Math.ceil()-소수점 첫째자리 올림: "+Math.ceil(12.3));
//3. Math.floor(double a)
System.out.println("3.Math.floor()-소수점 첫째자리 버림: "+Math.floor(12.6));
//4. Math.round(double a)
System.out.println("4.Math.round()-소수점 첫째자리 반올림: "+Math.round(12.3));
System.out.println("4.Math.round()-소수점 첫째자리 반올림: "+Math.round(12.6));
//※ 입력값 a를 소수점 n째 자리까지 표현
double a=12.34567;
int n=2;
System.out.println("※ 입력값 a를 소수점 n째 자리까지 표현: "+Math.round(a*Math.pow(10,n))/Math.pow(10,n));
//5. Math.max(double a, double b)
System.out.println("5.Math.max()-큰 수 구하기: "+Math.max(12,34));
//6. Math.min(double a, double b)
System.out.println("6.Math.min()-작은 수 구하기: "+Math.min(12,34));
//7. Math.pow(double a, double b)
System.out.println("7.Math.pow()-제곱수 구하기): "+Math.pow(3,3));
//8. Math.sqrt(double a)
System.out.println("8.Math.sqrt()-제곱근 구하기): "+Math.sqrt(16));
//9. Math.random()
System.out.println("9.Math.random()-임의의 수 구하기): "+Math.random());
System.out.println("9.Math.random()-1부터 100사이의 임의의 수 구하기): "+ (int)(Math.random()*101+1));
}
}
'Programming > Java' 카테고리의 다른 글
[정렬] 2. Java 배열(Array)의 정렬(Sorting) - 오름차순,내림차순,Comparable, Comparator (6) | 2022.11.12 |
---|---|
[정렬] 1. 버블 정렬(Bubble Sort) 알고리즘 (3) | 2022.11.11 |
[Java] 재귀함수 작성 방법 - 예제와 2가지 고려사항 (2) | 2022.11.04 |
[Java] Class ArrayList<E> 사용법 정리 (4) | 2022.11.01 |
코딩용 폰트가 따로 있다? 코딩용 폰트 비교 및 JAVA IDE 폰트 설정 (0) | 2022.10.07 |
댓글