50. Pow(x, n)

Implement pow(xn).

 

 public double MyPow(double x, int n) {
        if(n==0) return 1;
        double left = 1;
        double a = x;
        if(n == Int32.MinValue) 
        {
             n = Int32.MinValue+1;
             left *= x;
        }
        int b = (n>0)?n:-1*n;
        while(b >1)
        {
            if( b%2 != 0)  left *= a;
            a = a*a;
            b = b/2;
        }
        return (n>0)?a*left:1/(a*left);
    }

 

posted @ 2016-09-22 05:08  咖啡中不塌缩的方糖  阅读(104)  评论(0编辑  收藏  举报