JAVA Math클래스

Dev.Shinny·2022년 10월 6일
0

자바의 정석

목록 보기
16/18

Math 클래스

  • 기본적인 수학계산에 유용한 메서드로 구성.
  • 생성자의 접근제어자가 private이다.
  • 메서드는 모두 static이다.
  • E(자연로그), PI(원주율) 2개의 상수를 정의해 놓았다.

올림, 버림, 반올림

  • 올림 : Math.ceil()
  • 버림 : Math.floor()
  • 반올림 : Math.round() // 반환값 long
         Math.rint()  // 반환값 double
//소수점 첫째 자리에서 반올림
Math.round(double x); // long 반환
Math.round(float x); // int 반환
Math.round(3.15); // 결과 3

double num = 10.34567;
		
long roundResult1 = Math.round(num);
System.out.println(roundResult1);	 	// 10

double roundResult2 = Math.round(num*100)/100.0;
System.out.println(roundResult2); 		// 10.35

double roundResult13 = Math.round(num*1000)/1000.0;
System.out.println(roundResult13); 		// 10.346
			
double roundResult4 = Math.round(num*10000)/10000.0;
System.out.println(roundResult4); 		// 10.3457 

// 소수점 첫째 자리에서 올림
System.out.println(Math.ceil(10.1));	// 11.0
System.out.println(Math.ceil(10.9));	// 11.0

// 소수점 첫째 자리에서 버림
System.out.println(Math.floor(10.1));	// 10.0
System.out.println(Math.floor(10.9));	// 10.0
		

예외 발생

정수형간 연산에서 발생할 수 있는 오버플로우를 감지하기 위한 '-Exact'메소드

1) a + b
int addExact(int a, int b);

2) a - b
int subtractExact(int a, int b)

3) a * b 
int multiplyExact(int a, int b)

4) a++
int incrementExact(int a)

5) a--
int decrementExact(int a)

6) -a (부호 변경)
int negateExact(int a)

7) (int)value - int로 형변환
int toIntExact(long value)

삼각함수, 지수, 로그

  • 제곱 : pow( )
  • 제곱근(√ ) : sqrt( )
  • 삼각함수 : sin( ), cos( ), tan( )
입출력 모두 double1) pow() : x의 n 제곱을 구하는 함수
Math.pow((x),지수(n));
Math.pow(2,8); // 256.0 

2) sqrt() : x의 제곱근을 구하는 함수
Math.sqrt(x); 
Math.sqrt(4) // 2.0

두 점 (x1,y1), (x2, y2)간의 거리는 아래와 같다.

(x2x1)2+(y2y1)2\sqrt{(x2-x1)^2+(y2-y1)^2}

Math.sqrt(pow((x2-x1),2)+pow((y2-y1),2);

toRadians() : 각도를 라디안으로 변환
toDegrees() : 라디안을 각도 단위로 변환

- a = c x sinθ
- b = c x cosθ

1) 
double a = c * sin(PI/4); // PI/4 radian = 45 degree;
double b = c * cos(PI/4);

2) 
double a = c * sin(toRadians(45));
double b = c * cos(toRadians(45));
// toRadians 메서드 반환값 double

(1)(2)는 같은 코드이다. 

θ = inverse tan(a,b)

int a=1, b=1;
double b_angle = Math.toDegrees(Math.atan2(a,b));
System.out.println((int)b_angle); // 45 

24자리의 2진수가 10진수로 몇 자리의 값인지 구하는 공식은
 224=10x\ 2^{24}=10^x
양 변에 상용로그( log10\ log_{10})를 취하면,
x = 24×\timeslog102\log_{10}2 이 된다.
이것을 다시 코드화하면 아래와 같다.

24*Math.log10(2)

Math클래스 메서드

1) static abs()
주어진 값의 절대값을 반환한다.
int i = Math.abs(-10); 		// i = 10
double d = Math.abs(-10.0)  // d =10.0

2) static double ceil()
3) static double foor()

4) static max(변수1, 변수2)
주어진 두 값을 비교하여 큰 쪽을 반환한다.
double d = Math.max(9.5, 9.501); // d = 9.501
int i = Math.max(1, -1) 		 // i = 1

5) static min(변수1, 변수2)
 
6) static double random()
0.0~1.0 범위의 임의의 double값을 반환한다.
(1.0은 범위에 포함되지 않는다.)
   
double d = Math.random(); 			// 0.0 <= d <1.0
int i = (int)(Math.random()*10)+1   // 1 <= i < 11

7) static double rint(double a)
주어진 dobuble값을 반올림하며, 가운데 값 x.5는 짝수를 반환한다.
double d1 = Math.rint(1.2); // 1.0
double d2 = Math.rint(1.5); // 2.0
double d3 = Math.rint(1.8); // 2.0

8) static long round ()
profile
Hello I'm Shinny. A developer who try to enjoy the challenge.

0개의 댓글