지수와 로그

지수와 로그

제곱, 제곱근, 지수
제곱
같은 수를 두 번 곱함
거듭 제곱: 같은 수를 거듭하여 곱함
제곱근 (= root, √)
a를 제곱하여 b가 될 때 a를 b의 제곱근이라고 함
2^3=2×2×2
√4=√(2^2 )=2
𝑎^𝑥 → a: 밑, x: 지수

로그

log_𝑎⁡𝑏
a가 b가 되기 위해 제곱해야 하는 수
log_2⁡4=2
log_10⁡1000=3
log_𝑒⁡2.7182818284590452353602874713527=1

public class Practice6 {
    public static void main(String[] args) {
        //1.    제곱, 제곱근, 지수
        System.out.println("== 제곱 ==");
        System.out.println(Math.pow(2, 3));
        System.out.println(Math.pow(2, -3));
        System.out.println(Math.pow(-2, -3));

        System.out.println(Math.pow(2, 30));
        System.out.printf("%.0f\n", Math.pow(2, 30));

        System.out.println("== 제곱근 ==");
        System.out.println(Math.sqrt(16));
        System.out.println(Math.pow(16, 1.0 / 2));
        System.out.println(Math.pow(16, 1.0 / 4));

        //참고) 절대 값
        System.out.println("== 절대 값 ==");
        System.out.println(Math.abs(5));
        System.out.println(Math.abs(-5));

        //2.    로그
        System.out.println("== 로그 ==");
        System.out.println(Math.E);
        System.out.println(Math.log(2.718281828459045));
        System.out.println(Math.log10(1000));
        System.out.println(Math.log(4) / Math.log(2));
    }
}
== 제곱 ==
8.0
0.125
-0.125
1.073741824E9
1073741824
== 제곱근 ==
4.0
4.0
2.0
== 절대 값 ==
5
5
== 로그 ==
2.718281828459045
1.0
3.0
2.0
public class Practice6_1 {

    static double pow(int a, double b) {

        double result = 1;
        boolean isMinus = false;

        if (b == 0) {
            return 1;
        } else if (b < 0) {
            b *= -1;
            isMinus = true;
        }

        for (int i = 0; i < b; ++i) {
            result *= a;
        }

        return isMinus ? 1 / result : result;
    }

    static double sqrt(int a) {
        double result = 1;

        for (int i = 0; i < 10; ++i) {
            result = (result + (a / result)) / 2;
        }
        return result;
    }

    public static void main(String[] args) {
        //Test Code
        System.out.println("== Math pow ==");
        System.out.println(Math.pow(2, 3));
        System.out.println(Math.pow(2, -3));
        System.out.println(Math.pow(-2, -3));

        System.out.println("== My pow==");
        System.out.println(pow(2, 3));
        System.out.println(pow(2, -3));
        System.out.println(pow(-2, -3));

        System.out.println("== Math sqrt ==");
        System.out.println(Math.sqrt(16));
        System.out.println(Math.sqrt(8));

        System.out.println("== My sqrt ==");
        System.out.println(sqrt(16));
        System.out.println(sqrt(8));

    }
}
== Math pow ==
8.0
0.125
-0.125
== My pow==
8.0
0.125
-0.125
== Math sqrt ==
4.0
2.8284271247461903
== My sqrt ==
4.0
2.82842712474619

출처 : 제로베이스

Leave a comment