๐Ÿ’ก ์ˆ˜์˜ ๊ฑฐ๋“ญ์ œ๊ณฑ ๊ฐ’ ๊ตฌํ•˜๊ธฐ : Math.pow(n, m)

๋ฐ•ํ˜„์•„ยท2024๋…„ 10์›” 16์ผ
0

๊ธฐ์ดˆ

๋ชฉ๋ก ๋ณด๊ธฐ
4/31

๐Ÿ’ก ์ˆ˜์˜ ๊ฑฐ๋“ญ์ œ๊ณฑ ๊ฐ’ ๊ตฌํ•˜๊ธฐ

Math.pow(๊ฐ’, ์ง€์ˆ˜)

: ์ฃผ์˜ํ•  ์ ์€ ๊ฒฐ๊ณผ๊ฐ’์ด doubleํ˜•์œผ๋กœ ์ถœ๋ ฅ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค!!
int๊ฐ’์œผ๋กœ ํ•„์š”ํ•˜๋‹ค๋ฉด ํ˜•๋ณ€ํ™˜์„ ํ•ด์ค˜์•ผํ•œ๋‹ค.

public class PowerExample {
    public static void main(String[] args) {
    
        // 2์˜ 3์ œ๊ณฑ (2^3)
        double result1 = Math.pow(2, 3);
        System.out.println("2์˜ 3์ œ๊ณฑ: " + result1); // 8.0

        // 5์˜ 2์ œ๊ณฑ (5^2)
        double result2 = Math.pow(5, 2);
        System.out.println("5์˜ 2์ œ๊ณฑ: " + result2); // 25.0

        // 10์˜ 0์ œ๊ณฑ (10^0)
        double result3 = Math.pow(10, 0);
        System.out.println("10์˜ 0์ œ๊ณฑ: " + result3); // 1.0

        // 3์˜ -1์ œ๊ณฑ (3^-1)
        double result4 = Math.pow(3, -1);
        System.out.println("3์˜ -1์ œ๊ณฑ: " + result4); // 0.3333...
    }
}

int๋กœ ํ˜•๋ณ€ํ™˜ ํ•˜๊ธฐ : (int)Math.pow(n,m)

public class PowerExample {
    public static void main(String[] args) {
        int n = 2;
        int n2 = 3;

        // Math.pow์˜ ๊ฒฐ๊ณผ๋ฅผ int๋กœ ๋ณ€ํ™˜
        int result = (int) Math.pow(n, n2);
        System.out.println("2์˜ 3์ œ๊ณฑ: " + result); // 8
    }
}

0๊ฐœ์˜ ๋Œ“๊ธ€