Raising to power in PHP
The caret is the bit-wise XOR operator in PHP. You need to use pow()
for integers.
What is ** Exponentiation operator in php?
It is exponentiation
operator.
Functionality :
It gives Result of raising one variable to the other varibale's power
So if we want nth power of any number for that this operator is used
Example :
$x = 5;
$y = 3;
$z = $x ** $y; //<----- raising 3 power of 5 i.e we can say 5 raise to 3 in simple language . $x = base and $y= It is `exponentiation` operator.
echo $z; //results 125
How I can create my own pow function using PHP?
Oopsika, couldn't have asked a more obvious question. Use the built-in function named pow (as in a lot of languages)
echo pow(3, 3);
Edit
Let's create our own function.
function raiseToPower($base,$exponent)
{
// multiply the base to itself exponent number of times
$result=1;
for($i=1;$i<=$exponent;$i++)
{
$result = $result * $base;
}
return $result;
}
Exponents without using the pow() function
I don’t really know how to describe what’s wrong, here – your code doesn’t make sense for calculating an exponent. Start with 1, multiply by the base $exponent
times.
$result = 1;
for ($i = 0; $i < $exponent; $i++) {
$result *= $base;
}
echo $result;
PHP: How to raise number to (tiny) fractional exponent?
Your best bet is probably to use the Taylor series expansion. As you noted, PHP's bcpow is limited to raising to integer exponentiation.
So what you can do is roll your own bc factorial function and use the wiki page to implement a Taylor series expansion of the exponential function.
function bcfac($num) {
if ($num==0) return 1;
$result = '1';
for ( ; $num > 0; $num--)
$result = bcmul($result,$num);
return $result;
}
$mysum = '0';
for ($i=0; $i<300; $i++) {
$mysum = bcadd($mysum, bcdiv(bcpow($pow,$i), bcfac($i)) );
}
print $mysum;
Obviously, the $i<300
is an approximation for infinity... You can change it to suit your performance needs.
With $i=20
, I got
1.00000000000000000010842021724855044340662275184110560868263421994092888869270293594926619547803962155136242752708629105688492780863293090291376157887898519458498571566021915144483905034693109606778068801680332504212458366799913406541920812216634834265692913062346724688397654924947370526356787052264726969653983148004800229537555582281617497990286595977830803702329470381960270717424849203303593850108090101578510305396615293917807977774686848422213799049363135722460179809890014584148659937665374616
This is comforting since that small of an exponent should yield something really close to 1.0.
Returning the next nearest power of 2 for the given integer number
Try this :
pow(2,ceil(log($number,2)))
A more effective one :
function next_pow($number)
{
if($number < 2) return 1;
for($i = 0 ; $number > 1 ; $i++)
{
$number = $number >> 1;
}
return 1<<($i+1);
}
php pow function returns NAN
After some consultation with the experts in Room #11, pow()
won't work with the root of a negative number.
Should the answer be 0.8215
or -0.8215
?
Quirkily, using the **
(power) operator will work with the root of a negative number
echo -0.3741569180353 ** 0.2 ;
because **
has higher precedence than -
, so effectively what you're doing is
echo -(0.3741569180353 ** 0.2) ;
PHP Power of a number
I think I have an approach. It hinges on three simple observations:
- BCMath library contains a
sqrt
function - You can break
pow(x, a)
intopow(x, int(a)) * fracpow(x, frac(a))
wherepow
exists in the BCMath library, andfracpow
is a function we need to create (for an exponenta
between 0 and 1) A factional power can be expressed as the product of a number of square roots:
x^(0.22) = x^(1/8 + 1/16 + 1/32 + ...) = sqrt(sqrt(sqrt(x)))) * sqrt(sqrt(sqrt(sqrt(x)))) * ...
In other words - the approach to take is the following:
- Find the fractional part of the exponent
- Express that in powers of 2 (binary representation, basically)
- Keep taking the square root of your number
- Multiply your result by the new square root every time your factor should be included
This is not a fast method, and I suspect you need quite a few more digits of precision in the intermediate stages in order to ensure sufficient accuracy in the final result. But it will work...
If you need help implementing this let me know - I may have time later (or one of the other people looking at this answer may be inspired to write the code).
How to print power series in php
You can do this:
function exponentArr($num){
$arr = array();
for($i=0;$i <= $num;$i++){
$arr[$i] = pow(2, $i);
}
return $arr;
}
This will give you an array $arr
with the required output.
Related Topics
Table Name as Parameter Using Pdo/MySQL Prepared Statement
Imagick Not Loading Images, with 'Nodecodedelegateforthisimageformat' Error Message
How to Send Email with PDF Attachment Using PHP
Regular Expression for Checking Website Url
Loop Through Wordpress Posts, and Wrap Each X Post in a Div
How to Add a View Helper Directory (Zend Framework)
Discount for Certain Category Based on Total Number of Products
Count of Duplicate Elements in an Array in PHP
Htaccess Rule to Parse PHP in HTML Files Not Working
How to Use MySQLi_Fetch_Object with a Prepared Statement
How to Execute PHP Code Periodically in an Automatic Way
Change Pay Button on Checkout Based on Woocommerce Chosen Payment Method
How to Convert Week Number and Year into Unix Timestamp
PHP Values of One Array to Key of Another Array
Woocommerce: Change HTML Structure of Billing Fields in Checkout
How to Get Column Names from Pdo's Fetchall Result