Java Math
Java Math: 자바 수학 연산 가이드
Java에서 수학적 계산을 쉽게 할 수 있도록 다양한 수학 관련 기능을 제공하는 클래스가 있습니다. Math
클래스는
Java의 기본 클래스 중 하나로, 자주 사용되는 수학 연산과 관련된 메소드를 포함하고 있습니다. 이 클래스는 **정적 메소드(static method)**로 제공되므로, 객체를 생성하지 않고도 직접 호출할
수 있습니다.
이 가이드는 Java Math
클래스의 주요 메소드와 그 사용법을 설명합니다.
1. Math
클래스의 주요 메소드
1.1. 절대값 (abs()
)
abs()
메소드는 주어진 값의 절대값을 반환합니다.
public class MathAbs {
public static void main(String[] args) {
int a = -10;
double b = -5.75;
System.out.println(Math.abs(a)); // 출력: 10
System.out.println(Math.abs(b)); // 출력: 5.75
}
}
1.2. 최대값 (max()
) / 최소값 (min()
)
max()
메소드는 두 값 중 큰 값을 반환합니다.min()
메소드는 두 값 중 작은 값을 반환합니다.
public class MathMaxMin {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("최대값: " + Math.max(a, b)); // 출력: 20
System.out.println("최소값: " + Math.min(a, b)); // 출력: 10
}
}
1.3. 거듭제곱 (pow()
)
pow()
메소드는 첫 번째 인수를 두 번째 인수만큼 거듭제곱한 값을 반환합니다.
public class MathPow {
public static void main(String[] args) {
System.out.println(Math.pow(2, 3)); // 출력: 8.0 (2^3)
System.out.println(Math.pow(5, 2)); // 출력: 25.0 (5^2)
}
}
1.4. 제곱근 (sqrt()
)
sqrt()
메소드는 주어진 값의 제곱근을 반환합니다.
public class MathSqrt {
public static void main(String[] args) {
System.out.println(Math.sqrt(16)); // 출력: 4.0
System.out.println(Math.sqrt(25)); // 출력: 5.0
}
}
1.5. 랜덤 값 생성 (random()
)
random()
메소드는 0.0 이상 1.0 미만의 난수를 반환합니다. 일반적으로 랜덤 값을 생성할 때 범위를 조절하기 위해
사용됩니다.
public class MathRandom {
public static void main(String[] args) {
// 0.0 이상 1.0 미만의 랜덤 값
double randomValue = Math.random();
System.out.println(randomValue);
// 1부터 100까지의 랜덤 값
int randomInt = (int) (Math.random() * 100) + 1;
System.out.println(randomInt); // 1에서 100 사이의 값 출력
}
}
1.6. 반올림 (round()
)
round()
메소드는 주어진 값을 반올림하여 가장 가까운 정수로 반환합니다.
public class MathRound {
public static void main(String[] args) {
System.out.println(Math.round(5.5)); // 출력: 6
System.out.println(Math.round(5.4)); // 출력: 5
}
}
1.7. 올림 (ceil()
)
ceil()
메소드는 주어진 값을 올림하여 가장 가까운 정수로 반환합니다. 항상
크거나 같은 정수로 올림합니다.
public class MathCeil {
public static void main(String[] args) {
System.out.println(Math.ceil(5.1)); // 출력: 6.0
System.out.println(Math.ceil(-4.8)); // 출력: -4.0
}
}
1.8. 내림 (floor()
)
floor()
메소드는 주어진 값을 내림하여 가장 가까운 정수로 반환합니다. 항상
작거나 같은 정수로 내림합니다.
public class MathFloor {
public static void main(String[] args) {
System.out.println(Math.floor(5.9)); // 출력: 5.0
System.out.println(Math.floor(-4.2)); // 출력: -5.0
}
}
1.9. 삼각 함수 (sin()
, cos()
, tan()
)
sin()
,cos()
,tan()
메소드는 주어진 라디안 값에 대한 삼각 함수 값을 계산합니다.
public class MathTrig {
public static void main(String[] args) {
double radians = Math.toRadians(45); // 45도 -> 라디안으로 변환
System.out.println("sin(45°): " + Math.sin(radians)); // 출력: 0.7071...
System.out.println("cos(45°): " + Math.cos(radians)); // 출력: 0.7071...
System.out.println("tan(45°): " + Math.tan(radians)); // 출력: 1.0
}
}
1.10. 로그 함수 (log()
)
log()
메소드는 주어진 값의 **자연 로그 (base e)**를 계산합니다.
public class MathLog {
public static void main(String[] args) {
System.out.println(Math.log(1)); // 출력: 0.0
System.out.println(Math.log(Math.E)); // 출력: 1.0 (log(e))
}
}
2. Math
클래스의 상수
Math
클래스에는 자주 사용되는 수학 상수들이 정의되어 있습니다.
2.1. 원주율 (PI
)
- *
Math.PI
*는 원주율인 3.14159... 값을 제공합니다.
public class MathPI {
public static void main(String[] args) {
double radius = 5.0;
double area = Math.PI * Math.pow(radius, 2); // 원의 넓이 계산
System.out.println("원의 넓이: " + area); // 출력: 78.53981633974483
}
}
2.2. 자연상수 (E
)
- *
Math.E
*는 자연 로그의 밑인 2.71828... 값을 제공합니다.
public class MathE {
public static void main(String[] args) {
System.out.println("자연상수 e의 값: " + Math.E); // 출력: 2.718281828459045
}
}
3. Math
클래스 사용 예시
3.1. 원의 넓이와 둘레 계산
public class CircleMath {
public static void main(String[] args) {
double radius = 7.0;
// 원의 넓이: πr²
double area = Math.PI * Math.pow(radius, 2);
System.out.println("원의 넓이: " + area);
// 원의 둘레: 2πr
double circumference = 2 * Math.PI * radius;
System.out.println("원의 둘레: " + circumference);
}
}
3.2. 난수 생성과 범위 조절
public class RandomNumber {
public static void main(String[] args) {
// 1부터 10까지의 난수 생성
int randomNum = (int) (Math.random() * 10) + 1;
System.out.println("1부터 10까지의 난수: " + randomNum);
}
}
3.3. 삼각형의 빗변 계산 (피타고라스 정리)
public class Pythagoras {
public static void main(String[] args) {
double a = 3.0;
double b = 4.0;
// c² = a² + b² => c = √(a² + b²)
double c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
System.out.println("빗변의 길이: " + c); // 출력: 5.0
}
}
요약
Java의 Math
클래스는 다양한 수학적 연산을 제공하는 강력한 도구입니다. 이를 사용하여 절대값,
최대값/최소값, 거듭제곱, 제곱근, **
랜덤 값 생성**, 반올림, 삼각 함수 등을 쉽게 계산할 수 있습니다. 또한, **원주율(PI
)**과
자연상수(E
) 같은 상수도 제공되어 복잡한 수학적 계산을 간단하게 처리할 수 있습니다. Math
클래스를 잘 활용하면 수학적 계산이
필요한 모든 상황에서 효율적으로 문제를 해결할 수 있습니다.