메서드/설명 | 예제 | 결과 |
import java.lang.math; (java.lang 패키지는 자동으로 import) |
||
static double abs(double a) static float abs(float f) static int abs(int f) static long abs(long f) 주어진 값의 절대값을 반환한다. |
int i = Math.abs(-10); double d = Math.abs(-10.0); |
i = 10 d = 10.0 |
static double ceil(doulbe a) 주어진 값을 올림하여 반환한다. |
double d = Math.ceil(10.1); double d2 = Math.ceil(-10.1); double = Math.ceil(10.000015); |
d = 11.0 d2 = -10.0 d3 = 11.0 |
static double floor(double a) 주어진 값을 버림하여 반환한다. |
double d = Math.floor(10.8); double d2 = Math.floor(-10.8); |
d = 10.0 d2 = -11.0 |
static double max(double a, double b) static float max(float a, float b) static int max(int a, int b) static long max(long a, long b) 주어진 두 값을 비교하여 큰 쪽을 반환한다. |
double d = Math.max(9.5, 9.50001); int i = Math.max(0, -1); |
d = 9.50001 i = 0 |
static double min(double a, double b) static float min(float a, float b) static int min(int a, int b) static long min(long a, long b) 주어진 두 값을 비교하여 작은 쪽을 반환한다. |
double d = Math.min(9.5, 9.50001); int i = Math.min(0, -1); |
d = 9.5 i = -1 |
static double random() 0.0~1.0 범위의 임의의 double 값을 반환한다.(1.0은 범위에 포함되지 않는다.) |
double d = Math.random(); int i = (int)(Math.random()*10)+1 |
0.0 <= d < 1.0 1 <= i < 11 |
static double rint(double a) 주어진 double값과 가장 가까운 정수값을 double형으로 반환한다. 단, 두 정수의 정가운데 있는 값(1.5, 2.5, 3.5 등)은 짝수를 반환 |
double d = Math.rint(1.2); double d2 = Math.rint(2.6); double d3 = Math.rint(3.5); double d4 = Math.rint(4.5); double d5 = Math.rint(-1.5); double d6 = Math.rint(-1.4); |
d = 1.0 d2 = 3.0 d3 = 4.0 d4 = 4.0 d5 = -2.0 d6 = -1 |
static long round(double a) static long round(float a) 소수점 첫째자리에서 반올림한 정수값(long)을 반환한다. 두 정수의 정가운데있는 값은 항상 큰 정수를 반환.(rint()의 결과와 비교) |
long l = Math.round(1.2); long l2 = Math.round(2.6); long l3 = Math.round(3.5); long l4 = Math.round(4.5); long l5 = Math.round(-1.1); long l6 = Math.round(-1.8); double d = 90.7552; double d2 = Math.round(d*100)/100.0; |
l = 1 l2 = 3 l3 = 4 l4 = 5 l5 = -1 l6 = -2 d = 90.7552 d2 = 90.76 |
static double sqrt(double b) 주어진 값의 제곱근 값을 반환 |
double d1 = Math.sqrt(0); double d2 = Math.sqrt(1); double d3 = Math.sqrt(9); double d4 = Math.sqrt(0.64); double d5 = Math.sqrt(-9); |
d1 = 0.0 d2 = 1.0 d3 = 3.0 d4 = 0.8 d5 = NaN |
static double pow(double base, double exponent) 주어진 값의 n승을 반환 |
double d1 = Math.pow(2, 8); double d2 = Math.pow(3, 4); double d3 = Math.pow(9, 0.5); double d4 = Math.pow(8, -1); double d5 = Math.pow(10, -2); |
d1 = 256.0 d2 = 81.0 d3 = 3.0 d4 = 0.125 d5 = 0.01 |
static double sin(double angle) 라디언값을 받아서 사인값을 반환 |
double d1 = Math.sin(3); double d2 = Math.sin(-3); double d3 = Math.sin(0); double d4 = Math.sin(Math.PI); double d5 = Math.sin(Math.PI/2); |
d1 = 0.1411200080598672 d2 = -0.1411200080598672 d3 = 0.0 d4 = 1.2246467991473532E-16 d5 = 1.0 |
static double cos(double angle) 라디언값을 받아서 코사인값을 반환 |
double d1 = Math.cos(3); double d2 = Math.cos(-3); double d3 = Math.cos(0); double d4 = Math.cos(Math.PI); double d5 = Math.cos(Math.PI/2); |
d1 = -0.9899924966004454 d2 = -0.9899924966004454 d3 = 1.0 d4 = -1.0 d5 = 6.123233995736766E-17 |
static double tan(double angle) 라디언값을 받아서 탄젠트값을 반환 |
double d1 = Math.tan(0.5); double d2 = Math.tan(-0.5); double d3 = Math.tan(1); double d4 = Math.tan(-3); double d5 = Math.tan(Math.PI/4); |
d1 = 0.5463024898437905 d2 = -0.5463024898437905 d3 = 1.5574077246549023 d4 = 0.1425465430742778 d5 = 0.9999999999999999 |
static double atan2(double y, double x) 직각 삼각형에서 a, b 를 알면 끼인각(각도)를 구해준다. 결과값은 라디안이므로 도(degree)단위로 변환하려면 toDegrees() 메서드를 이용하면된다. |
double c = Math.sqrt(2); double a = c * sin(Math.toRadians(45)); double b = c * cos(Math.toRadians(45)); double degree = Math.atan2(a,b); |
degree = 0.7853981633974482 Math.toDegrees(degree) = 44.999999999999 |
static double toRadians(double angle) 주어진 각도를 "라디안"으로 변환 |
double d1 = Math.toRadians(90); double d2 = Math.toRadians(45); |
d1 = 1.5707963267948966 d2 = 0.7853981633974483 |
static double toDegrees(double angle) 주어진 라디안 값을 각도값으로 변환 |
double d1 = Math.toRadians(90); double d2 = Math.toDegrees(d1); |
d1 = 1.5707963267948966 d2 = 90.0 |
참고자료
[1] [< Hyun / Log >:티스토리] - [Java/자바] Math 클래스 정리 (feat.삼각함수)
'🖥️ Backend > Java' 카테고리의 다른 글
List 인터페이스 추상 메서드 총 정리 (0) | 2025.05.01 |
---|---|
Collection 인터페이스 추상 메서드 총 정리 (0) | 2025.05.01 |
StringTokenizer 클래스 메서드 총 정리 (0) | 2025.04.27 |
StringJoiner 클래스 메서드 총 정리 (0) | 2025.04.26 |
StringBuffer 클래스 메서드 총 정리 (0) | 2025.04.24 |