JAVA (10) Math 클래스Java2022. 12. 4. 11:20
Table of Contents
728x90
728x90
Math 클래스
- 수학과 관련된 일련의 작업들을 수행할 수 있는 클래스(java.lang 패키지안에 포함)
- Math클래스의 다양한 메서드들은 static으로 구현되어 있어 따로 객체를 생성하지 않고 사용 가능
※ java.lang 패키지
- java 프로그래밍에 필요한 가장 기본적인 클래스들이 모여있는 패키지
- import구문 없이도 자동으로 프로그램에 포함
1. abs
- 주어진 값의 절대값을 반환
System.out.println(Math.abs(10)); //10
System.out.println(Math.abs(-10)); //10
System.out.println(Math.abs(-3.1037f));//3.1037
2. max / min
- 두 값중 더 큰 값, 작은 값을 반환
System.out.println(Math.max(1234, 123)); // 1234
System.out.println(Math.min(1234, 123)); // 123
3. ceil / floor / round / floorDiv
- ceil, floor, round : 올림, 내림, 반올림
- floorDiv : 몫보다 작거나 같은 가장 큰 정수 값 반환
System.out.println(Math.ceil(10.4)); // 11.0
System.out.println(Math.floor(10.5)); // 10
System.out.println(Math.round(10.5)); // 3.1037
// 이런식으로 활용 가능 ( 자릿수 지정 )
System.out.println(Math.ceil(10.454545*10)/10); // 10.5
System.out.println(Math.floor(10.545454*1000)/1000); // 10.555
// floorDiv
System.out.println(Math.floorDiv(100, 3)); // 33
System.out.println(Math.floorDiv(3, 100)); // 0
4. sqrt / cbrt
- 제곱근, 세제곱근
System.out.println(Math.sqrt(25)); // 5.0
System.out.printf("%.4f",Math.cbrt(25)); // 2.9240
5. pow
- 제곱 함수 ( pow(밑,지수) )
System.out.println(Math.pow(2,5)); // 32
6. signum
- 주어진 값의 부호 출력
System.out.println(Math.signum(1.3f)); // 1.0
System.out.println(Math.signum(0)); // 0.0
System.out.println(Math.signum(-3.14)); //-1.0
7. copySign / nextAfter / nextUp / nextDown / ulp
- copySign : 두 번째 인수에 지정된 부호 + 첫 번째 인수의 절대값 반환
- nextAfter(x,y) : y를 향한 x 다음의 부동 소수점 값을 반환, x=y이면 y반환
- nextUp : 양의 무한대방향으로 인접한 부동 소수점 값을 반환
- nextDown : 음의 무한대방향으로 인접한 부동 소수점 값을 반환
- ulp : float x의 최하위 비트 반환
System.out.println(Math.copySign(-3,2)); // 3.0
System.out.println(Math.nextAfter(-3.1,2)); // -3.0999999999999996
System.out.println(Math.nextUp(-3.1)); // -3.0999999999999996
System.out.println(Math.nextDown(-3.1)); // -3.1000000000000005
System.out.println(Math.ulp(3f)); // 2.3841858E-7
8. random
- 난수 생성
- 범위 미지정 시 0.0~1.0사이의 double값 반환
- 범위 지정 방법 : (Math.random()*x)+y
// 범위 미지정 시
for(int i=0; i<10; i++) {
System.out.println(Math.random());
}
0.9016855235047694
0.615614968623635
0.04077792462493879
0.7617507858319714
0.9148626832492266
0.3241254670110941
0.3912494309871509
0.9820537595650879
0.9322974745562934
0.0019263079896513213
// 원하는 정수 범위의 random을 만들어보자
for(int i=0; i<10; i++) {
System.out.print((int)(Math.random()*10)+1 + " ");
}
>> 5 3 3 1 10 4 4 5 8 8
9. rint
- 인자 값에 가장 가까운 정수를 double값으로 반환
System.out.println(Math.rint(3.33333333)); // 3.0
System.out.println(Math.rint(3.55555555)); // 4.0
10. hypot
- sqrt(x²+y²)의 값
System.out.println(Math.hypot(2, 2));
System.out.println(Math.sqrt(8));
// 두 값은 같다
// hypot(2,2)=sqrt(2²+2²) = 2.8284271247461903
11. getExponent / IEEEremainder ( ? )
- getExponent : 편향되지 않은 지수를 반환
- IEEEremainder : IEEE 754 규정된 두 인수에 대한 나머지 연산을 계산
double a = 345.65;
double b = 1.0 / 0;
double c = 0;
float d = 237.2f;
float e = 1.0f / 0;
float f = 0f;
System.out.println(Math.getExponent(a)); // 8
System.out.println(Math.getExponent(b)); // 1024
System.out.println(Math.getExponent(c)); // -1023
System.out.println(Math.getExponent(d)); // 7
System.out.println(Math.getExponent(e)); // 128
System.out.println(Math.getExponent(f)); // -127
double a1 = 31.34;
double a2 = 2.2;
double b1 = -21.0;
double b2 = 7.0;
double c1 = 1.0 / 0;
double c2 = 0.0;
double d1 = -2.34;
double d2 = 1.0 / 0;
System.out.println(Math.IEEEremainder(a1, a2)); // 0.5399999999999974
// b1와 출력은 음수 0
System.out.println(Math.IEEEremainder(b1, b2)); // -0.0
// 첫 번째 인수는 무한대이고 두 번째 인수는 0
System.out.println(Math.IEEEremainder(c1, c2)); // NaN
// 첫 번째 인수는 유한하고 두 번째 인수는 무한대
System.out.println(Math.IEEEremainder(d1, d2)); // -2.34
12. Exact ( JAVA 8부터 추가 )
- addExact : 더하기
- subtractExact : 빼기
- multiplyExact : 곱하기
- incrementExact : 값 1 증가
- decrementExact : 값 1 감소
- negateExact : 부호 변경
- tolnExact : long 타입을 int로 변경
※ 인수값으로 int / long이 오버로딩되어있으며 오버플로우된 경우 예외가 발생
System.out.println(Math.addExact(2, 3)); // 5
System.out.println(Math.subtractExact(2,3)); // -1
System.out.println(Math.multiplyExact(2,3)); // 6
System.out.println(Math.incrementExact(3)); // 4
System.out.println(Math.decrementExact(3)); // 2
System.out.println(Math.negateExact(3)); // -3
// int 범위를 넘어가는 값을 넘기면 오버플로우가 발생
double a = 9223372036854775807L;
System.out.println(Math.toIntExact(a));
13. log
- log : log를 반환
- log10 : 밑이 10인 로그를 반환
- log1p : 인수와 1의 합의 로그를 반환 (log(x)=log1p(x-1))
System.out.println(Math.log(100)); // 4.605170185988092
System.out.println(Math.log10(100)); // 2.0
System.out.println(Math.log1p(99)); // 4.605170185988092
// 메서드 활용
public class BaseLog
static double baseLog(int x, int base) {
return Math.log10(x) / Math.log10(base);
}
public static void main(String[] args){
System.out.println(baseLog(8,2)); // 3.0
System.out.println(baseLog(9,3)); // 2.0
}
14. exp / expm1
- exp : 입력받은 인자의 값에서 E(오일러 수)를 거듭제곱 / E ≒ (2.71828)
- expm1 : E의 거듭제곱을 계산하고 1을 뺀다
// Math.exp(a) = a의 E승
System.out.println(Math.exp(1)); // 2.718281828459045
System.out.println(Math.expm1(1)); // 1.718281828459045
15. 삼각법 ( sin / cos / tan / asin / acos / atan )
- 주어진 double 값의 사인,코사인,탄젠트, 아크사인, 아크코사인, 아크탄젠트 값을 반환
System.out.println(Math.sin(1)); // 0.8414709848078965
System.out.println(Math.cos(1)); // 0.5403023058681398
System.out.println(Math.tan(1)); // 1.5574077246549023
System.out.println(Math.asin(1)); // 1.5707963267948966
System.out.println(Math.acos(1)); // 0.0
System.out.println(Math.atan(1)); // 0.7853981633974483
16. 쌍곡선 ( sinh / cosh / tanh )
- 주어진 double 값의 쌍곡선 코사인 / 사인 / 탄젠트 값을 반환
System.out.println(Math.sinh(1)); // 1.1752011936438014
System.out.println(Math.cosh(1)); // 1.543080634815244
System.out.println(Math.tanh(1)); // 0.7615941559557649
17. 각도
- toDegrees : 라디안 각도를 일반 각도로
- toRadians : 일반 각도(0~360˚)를 라디안 각도로
System.out.println(Math.toRadians(360)); // 6.283185307179586
System.out.println(Math.toDegrees(6.283185307179586)); // 360
728x90
300x250
@mag1c :: 꾸준히 재밌게
2023.04 ~ 백엔드 개발자의 기록
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!