Home  >  Article  >  Backend Development  >  How to implement integer power of numerical value in php (code example)

How to implement integer power of numerical value in php (code example)

不言
不言forward
2018-11-17 17:35:272917browse

The content this article brings to you is about how PHP realizes the integer power of numerical values ​​(code examples). It has certain reference value. Friends in need can refer to it. I hope it will be useful to you. Helps.

Given a floating-point number base of type double and an integer exponent of type int. Find the exponent power of base.

Idea:

1. The binary expression of the exponent to the power of 10^6 can represent 10^110 (binary) 10^100 * 10^10 * 10^000=>10^4 * 10^2
2. Shift operation

while(n!=0){
    if((n&1)==1)
        res*=curr;
    curr*=curr;// 翻倍
    n>>=1;// 右移一位
}

8fe4e1556940fd138e485a1fe90ad3d20){
        $exponent = $n; 
    }else if($n7aed4bb32ad43bd8a449cb9fa41170de>=1;// 右移一位
    }   
    return $n>=0?$res:(1/$res);//指数是负数的情况 
}

$a=Power(10,6);
var_dump($a);
~

The above is the detailed content of How to implement integer power of numerical value in php (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete