Responsive Advertisement

Java Math ํด๋ž˜์Šค: ์ˆ˜ํ•™ ์—ฐ์‚ฐ์˜ ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ

Java์˜ Math ํด๋ž˜์Šค๋Š” ์ˆ˜ํ•™์  ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์ธ ์‚ฐ์ˆ  ์—ฐ์‚ฐ๋ถ€ํ„ฐ ๋ณต์žกํ•œ ์‚ผ๊ฐ ํ•จ์ˆ˜, ๋กœ๊ทธ ํ•จ์ˆ˜, ์ง€์ˆ˜ ์—ฐ์‚ฐ๊นŒ์ง€ ๋‹ค์–‘ํ•œ ์ž‘์—…์„ ๊ฐ„ํŽธํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” Math ํด๋ž˜์Šค์˜ ์ฃผ์š” ๊ธฐ๋Šฅ๊ณผ ํ™œ์šฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

1. Math ํด๋ž˜์Šค์˜ ๊ฐœ์š”

Math ํด๋ž˜์Šค๋Š” java.lang ํŒจํ‚ค์ง€์— ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉฐ, ๋ณ„๋„์˜ ์ž„ํฌํŠธ ์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๋ฉ”์„œ๋“œ๋Š” ์ •์  ๋ฉ”์„œ๋“œ(Static Method)๋กœ ์ •์˜๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ, ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ํ˜ธ์ถœ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

Math ํด๋ž˜์Šค์˜ ์ฃผ์š” ์ƒ์ˆ˜

  • Math.PI: ์›์ฃผ์œจ(์•ฝ 3.14159)
  • Math.E: ์ž์—ฐ ๋กœ๊ทธ์˜ ๋ฐ‘(์•ฝ 2.718)

์˜ˆ์ œ

public class MathConstantsExample {
    public static void main(String[] args) {
        System.out.println("PI: " + Math.PI);
        System.out.println("E: " + Math.E);
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

PI: 3.141592653589793
E: 2.718281828459045

2. Math ํด๋ž˜์Šค์˜ ์ฃผ์š” ๋ฉ”์„œ๋“œ

Math ํด๋ž˜์Šค๋Š” ๋‹ค์–‘ํ•œ ์ˆ˜ํ•™ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ๋“ค์ž…๋‹ˆ๋‹ค.

2.1 ์ ˆ๋Œ“๊ฐ’: abs()

abs() ๋ฉ”์„œ๋“œ๋Š” ์ˆซ์ž์˜ ์ ˆ๋Œ“๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

public class MathAbsExample {
    public static void main(String[] args) {
        System.out.println("์ ˆ๋Œ“๊ฐ’ (์ •์ˆ˜): " + Math.abs(-10));
        System.out.println("์ ˆ๋Œ“๊ฐ’ (์‹ค์ˆ˜): " + Math.abs(-3.14));
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

์ ˆ๋Œ“๊ฐ’ (์ •์ˆ˜): 10
์ ˆ๋Œ“๊ฐ’ (์‹ค์ˆ˜): 3.14

2.2 ์˜ฌ๋ฆผ๊ณผ ๋‚ด๋ฆผ: ceil()์™€ floor()

ceil() ๋ฉ”์„œ๋“œ๋Š” ์ˆซ์ž๋ฅผ ์˜ฌ๋ฆผํ•˜๋ฉฐ, floor() ๋ฉ”์„œ๋“œ๋Š” ์ˆซ์ž๋ฅผ ๋‚ด๋ฆผํ•ฉ๋‹ˆ๋‹ค.

public class MathCeilFloorExample {
    public static void main(String[] args) {
        System.out.println("์˜ฌ๋ฆผ: " + Math.ceil(3.14));
        System.out.println("๋‚ด๋ฆผ: " + Math.floor(3.14));
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

์˜ฌ๋ฆผ: 4.0
๋‚ด๋ฆผ: 3.0

2.3 ๋ฐ˜์˜ฌ๋ฆผ: round()

round() ๋ฉ”์„œ๋“œ๋Š” ์ˆซ์ž๋ฅผ ๋ฐ˜์˜ฌ๋ฆผํ•ฉ๋‹ˆ๋‹ค.

public class MathRoundExample {
    public static void main(String[] args) {
        System.out.println("๋ฐ˜์˜ฌ๋ฆผ: " + Math.round(3.14));
        System.out.println("๋ฐ˜์˜ฌ๋ฆผ: " + Math.round(3.74));
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

๋ฐ˜์˜ฌ๋ฆผ: 3
๋ฐ˜์˜ฌ๋ฆผ: 4

2.4 ๊ฑฐ๋“ญ์ œ๊ณฑ๊ณผ ์ œ๊ณฑ๊ทผ: pow()์™€ sqrt()

pow() ๋ฉ”์„œ๋“œ๋Š” ๊ฑฐ๋“ญ์ œ๊ณฑ์„ ๊ณ„์‚ฐํ•˜๋ฉฐ, sqrt() ๋ฉ”์„œ๋“œ๋Š” ์ œ๊ณฑ๊ทผ์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

public class MathPowSqrtExample {
    public static void main(String[] args) {
        System.out.println("2์˜ 3์ œ๊ณฑ: " + Math.pow(2, 3));
        System.out.println("16์˜ ์ œ๊ณฑ๊ทผ: " + Math.sqrt(16));
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

2์˜ 3์ œ๊ณฑ: 8.0
16์˜ ์ œ๊ณฑ๊ทผ: 4.0

2.5 ์ตœ๋Œ€๊ฐ’๊ณผ ์ตœ์†Œ๊ฐ’: max()์™€ min()

max() ๋ฉ”์„œ๋“œ๋Š” ๋‘ ๊ฐ’ ์ค‘ ๋” ํฐ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, min() ๋ฉ”์„œ๋“œ๋Š” ๋” ์ž‘์€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

public class MathMaxMinExample {
    public static void main(String[] args) {
        System.out.println("์ตœ๋Œ€๊ฐ’: " + Math.max(10, 20));
        System.out.println("์ตœ์†Œ๊ฐ’: " + Math.min(10, 20));
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

์ตœ๋Œ€๊ฐ’: 20
์ตœ์†Œ๊ฐ’: 10

2.6 ๋žœ๋ค ์ˆซ์ž ์ƒ์„ฑ: random()

random() ๋ฉ”์„œ๋“œ๋Š” 0.0 ์ด์ƒ 1.0 ๋ฏธ๋งŒ์˜ ๋‚œ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ™œ์šฉํ•ด ์›ํ•˜๋Š” ๋ฒ”์œ„์˜ ๋‚œ์ˆ˜๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

public class MathRandomExample {
    public static void main(String[] args) {
        int randomNumber = (int) (Math.random() * 100); // 0~99 ์‚ฌ์ด์˜ ๋‚œ์ˆ˜
        System.out.println("๋‚œ์ˆ˜: " + randomNumber);
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ (์˜ˆ์‹œ):

๋‚œ์ˆ˜: 42

2.7 ์‚ผ๊ฐ ํ•จ์ˆ˜: sin(), cos(), tan()

์‚ผ๊ฐ ํ•จ์ˆ˜ ๋ฉ”์„œ๋“œ๋Š” ๊ฐ๋„(๋ผ๋””์•ˆ ๋‹จ์œ„)๋ฅผ ์‚ฌ์šฉํ•ด ์‚ผ๊ฐ ํ•จ์ˆ˜ ๊ฐ’์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

public class MathTrigExample {
    public static void main(String[] args) {
        double radians = Math.toRadians(30); // 30๋„๋ฅผ ๋ผ๋””์•ˆ์œผ๋กœ ๋ณ€ํ™˜
        System.out.println("sin(30): " + Math.sin(radians));
        System.out.println("cos(30): " + Math.cos(radians));
        System.out.println("tan(30): " + Math.tan(radians));
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

sin(30): 0.49999999999999994
cos(30): 0.8660254037844387
tan(30): 0.5773502691896257

3. Math ํด๋ž˜์Šค ํ™œ์šฉ ํŒ

  • ์ •ํ™•ํ•œ ๊ฐ’ ๊ณ„์‚ฐ: Math ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋Š” ์ •ํ™•๋„ ๋†’์€ ๊ณ„์‚ฐ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
  • ๋‚œ์ˆ˜ ์ƒ์„ฑ: ๊ฒŒ์ž„, ์•”ํ˜ธํ•™, ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋“ฑ ๋‚œ์ˆ˜๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ์‚ผ๊ฐ ํ•จ์ˆ˜์™€ ๋กœ๊ทธ: ๊ณผํ•™ ๊ณ„์‚ฐ ๋ฐ ๋ฐ์ดํ„ฐ ๋ถ„์„์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

๊ฒฐ๋ก 

Java์˜ Math ํด๋ž˜์Šค๋Š” ์ˆ˜ํ•™์ ์œผ๋กœ ํ•„์š”ํ•œ ๋Œ€๋ถ€๋ถ„์˜ ์—ฐ์‚ฐ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ•๋ ฅํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์ธ ์—ฐ์‚ฐ๋ถ€ํ„ฐ ์‚ผ๊ฐ ํ•จ์ˆ˜์™€ ๋‚œ์ˆ˜ ์ƒ์„ฑ๊นŒ์ง€ ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜๋ฉด ํšจ์œจ์ ์ด๊ณ  ์ •ํ™•ํ•œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ธ€์—์„œ ์†Œ๊ฐœํ•œ ๋ฉ”์„œ๋“œ์™€ ํ™œ์šฉ๋ฒ•์„ ์ฐธ๊ณ ํ•˜์—ฌ, Java์—์„œ ์ˆ˜ํ•™์  ์ž‘์—…์„ ๋” ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•ด ๋ณด์„ธ์š”!

๋Œ“๊ธ€ ์“ฐ๊ธฐ