剑指offer 11 数值的整数次方

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Dennis_Wu_/article/details/79948636
package offer;

/**
 * 求数值的整数次方,需要考虑全面
 * 10为底时负数次幂无意义,0为指数值为1,1为指数值为其本身
 * 2、指数为负数时,注意为倒数
 * 3、计算机中判断相等时考虑精度的问题,自己定义equals方法
 *
 * @auther Dennis
 * @date 2018/4/15
 */
public class Problem11Power {

    private boolean equal(double num1, double num2) {
        if (num1 - num2 > -0.0000001 && num1 - num2 < 0.0000001) {
            return true;
        } else {
            return false;
        }
    }

    private double powerWithExponent(double base, int exponent) {
        if (exponent == 0)
            return 1;
        if (exponent == 1)
            return base;
        double result = 1.0;
        for (int i = 1; i <= exponent; i++) {
            result *= base;
        }
        return result;
    }

    public double power(double base, int exponent) throws Exception {
        double result = 0.0;
        if (equal(base, 0.0) && exponent < 0) {
            throw new Exception("0的负数次幂没有意义");
        }
        if (exponent < 0) {
            result = 1.0 / powerWithExponent(base, -exponent);
        } else {
            result = powerWithExponent(base, exponent);
        }
        return result;
    }

    public static void main(String[] args) throws Exception {
        Problem11Power p11 = new Problem11Power();
        System.out.println(p11.power(2.0, 3));
    }
}
阅读更多

没有更多推荐了,返回首页