java 指数计算

    /**
     * 给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
     */
    public static void testGetExponentOfNum() {

        double base = Math.random();
        int exponent = new Random().nextInt(5);
        System.out.println("testGetExponentOfNum--base=" + base+"--exponent="+exponent);
        if (exponent == 0) {
            System.out.println("testGetExponentOfNum--0--result=" + 1);
        } else if (exponent == 1) {
            System.out.println("testGetExponentOfNum--1--result=" + base);
        } else if (exponent > 1) {
            double num = base;
            for (int i = 2; i <= exponent; i++) {
                num = num * base;
            }
            System.out.println("testGetExponentOfNum-->1--result=" + num);
        } else {
            System.out.println("testGetExponentOfNum error");
        }
        System.out.println("testGetExponentOfNum use Api result="+Math.pow(base,exponent));
    }

写完以后突然发现上面的代码没有考虑base 与 exponent可能为负数的情况。

    /**
     * 给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
     */
    public static void testGetExponentOfNum01() {
        double base = Math.random() * (new Random().nextInt(1) > 0 ? 1 : -1);
        int exponent = new Random().nextInt(5) * (new Random().nextInt(1) > 0 ? 1 : -1);
        double result = 1;
        System.out.println("testGetExponentOfNum--base=" + base + "--exponent=" + exponent);
        if(base == 0){
            System.out.println("testGetExponentOfNum--base == 0");
            return;
        }
        if (exponent == 0) {
            System.out.println("testGetExponentOfNum--[=0]--result=" + 1);
            return;
        } else if (exponent > 0) {
            result = base;
            for (int i = 2; i <= exponent; i++) {
                result = result * base;
            }
            System.out.println("testGetExponentOfNum--[>0]--result=" + result);
        } else {
            result = base;
            for (int i = 2; i <= Math.abs(exponent); i++) {
                result = result * base;
            }
            result = 1 / result;
            System.out.println("testGetExponentOfNum--[<0]--result=" + result);
        }

        System.out.println("testGetExponentOfNum use Api result=" + Math.pow(base, exponent));
    }

RESULT

12-15 20:08:29.977 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum--base=-0.7797871217121436--exponent=-3
12-15 20:08:29.977 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum--[<0]--result=-2.1089769082499257
12-15 20:08:29.977 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum use Api result=-2.1089769082499257
12-15 20:08:29.977 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum==========
12-15 20:08:32.400 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum--base=-0.7498661439578588--exponent=-3
12-15 20:08:32.400 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum--[<0]--result=-2.3716399771135768
12-15 20:08:32.400 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum use Api result=-2.3716399771135768
12-15 20:08:32.400 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum==========
12-15 20:08:33.991 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum--base=-0.23920783483769525--exponent=-2
12-15 20:08:33.991 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum--[<0]--result=17.476288271069706
12-15 20:08:33.991 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum use Api result=17.476288271069706
12-15 20:08:33.991 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum==========
12-15 20:08:35.715 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum--base=-0.12243023418032972--exponent=0
12-15 20:08:35.715 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum--[=0]--result=1
12-15 20:08:35.715 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum use Api result=1.0
12-15 20:08:35.715 20837-20837/com.example.bxh.sayhello I/System.out: testGetExponentOfNum==========

====如果有错误,请指正====

你可能感兴趣的:(算法)