javascript 指数为整数 Math.pow

指数为整数

Math.ipow = function(x, y) {
    if (0==y) return 1;
    // else return x * pow(x, y-1);
    var res = 1;
    for (; y != 0; y>>=1) {
        if ((y&1) != 0) {
            res *= x;
        }
        x = x * x;
    }
    return res;
};

猜你喜欢

转载自blog.csdn.net/fareast_mzh/article/details/84562268