pow


php128 apg

RETURNS a number as base raised to the power of another number as expoent.

Compatible with LOCALE only, up to PHP 7.4.XX.





This function returns the $num raise to the power of $exponent.

If with $num and $exponent are non-negative integers and the result can be represented as an INTEGER, the result will be returned with INTEGER type, otherwise it will be returned as a FLOAT.

It is possible to use the ** operator instead.

This function could lead to weird results because it converts the entered values to non-scalar values.



<?php

int
|float|object pow ( mixed $num, mixed $exponent )


where,

$num = The number treated as base
            
$exponent 
= Another number treated as expoent
            
?>

$num


The base to be used.



$exponent


The expoente.



  1 EXERCISE   

<?php

$base 
= M_PI;

$exp = M_E;

$base2exp_a = pow($base, $exp);

$base2exp_b = $base**$exp;

setlocale(LC_ALL, "de_DE.utf-8", "de-DE.utf-8", "de-DE", "de");
 
echo 
'pow(' . $base . ', ' . $exp . ') = ' . $base2exp_a . '<br><br>';

echo 
'The same as ( ' . $base . ' ** ' . $exp . ' ) = ' . 
                                       
$base2exp_b . '<br>';

?> 

  2 EXERCISE   

<?php

$base02 
= 2.3456;

$exp02 = -19.5;

$base2exp_a = pow($base02, $exp02);

$base2exp_b = $base02**$exp02;

// Compatible with LOCALE only, up to PHP 7.4.XX 

setlocale(LC_ALL, "de_DE.utf-8", "de-DE.utf-8","nds", 
                                   
"nds_DE", "de-DE", "de");
                                   
echo 
'pow(' . $base02 . ', ' . $exp02 . ') = ' . $base2exp_a . '<br><br>';


echo 
'The same as ( ' . $base02 . ' ** ' . $exp02 . ' ) = ' . 
                                     
$base2exp_b . '<br>';

?> 

  3 EXERCISE   

<?php

$base03a 
= M_PI;

$exp03a = 1/5;

$ex03b = 5;

$pow03a = pow($base03a, $exp03a); 
// fifth root of M_PI

echo 'pow( ' . $base03a . ', ' .  $exp03a . ') = ' . $pow03a . '<br><br><br>';

echo 
'This leads us to reverse operation:<br><br>';

$pow03b = pow($pow03a, 5);
// fifth root of M_PI raised to the fifth power

echo 'pow( ' . $pow03a . ', ' . $ex03b . ') = ' . $pow03b . '<br><br>';

?> 

  4 EXERCISE   

<?php

$large_exp 
= 20000;

echo 
"The following all result in INF:<br>";
var_dump(pow(24, $large_exp));
echo 
'<br><br>';
var_dump(pow(0.24, -$large_exp));
echo 
'<br><br>';
var_dump(pow(-0.24, -$large_exp));
echo 
'<br><br>';
echo 
"<br><br>The following all result in 0:<br>";
var_dump(pow(0.24, $large_exp));
echo 
'<br><br>';
var_dump(pow(-0.24, $large_exp));
echo 
'<br><br>';
var_dump(pow(24, -$large_exp));
echo 
'<br><br>';
var_dump(pow(-24, -$large_exp));
echo 
'<br><br>';
echo 
"<br><br>The following all result in -0:<br>";
var_dump(pow(-0.24, $large_exp+1));
echo 
'<br><br>';
echo 
"<br><br>The following all result in -INF:<br>";
var_dump(pow(-24, $large_exp+1));
echo 
'<br><br>';
var_dump(pow(-0.24, -$large_exp+1));

?>

  5 EXERCISE   

<?php

setlocale
(LC_ALL, [ 'pt-BR', 'pt_BR' ] );

if (
PHP_INT_SIZE != 8) 
die(
"skip this test is for 64bit platform only");

define("MAX_64Bit", 9223372036854775807);
define("MAX_32Bit", 2147483647);
define("MIN_64Bit", -9223372036854775807 - 1);
define("MIN_32Bit", -2147483647 - 1);

$longVals = array(
    
MAX_64Bit, MIN_64Bit, MAX_32Bit, 
    
MIN_32Bit, MAX_64Bit - MAX_32Bit, 
    
MIN_64Bit - MIN_32Bit,
    
MAX_32Bit + 1, MIN_32Bit - 1, 
    
MAX_32Bit * 2, (MAX_32Bit * 2) + 1, 
    (
MAX_32Bit * 2) - 1,
    
MAX_64Bit -1, MAX_64Bit + 1, 
    
MIN_64Bit + 1, MIN_64Bit - 1
);

$otherVals = array(0, 1, -1, 7, 9, 65, -44, 
MAX_32Bit, MIN_32Bit, MAX_64Bit, MIN_64Bit);


foreach (
$longVals as $longVal) {
   foreach(
$otherVals as $otherVal) {
       echo 
"<br><br>Testing: $longVal, $otherVal<br>";
      
var_dump(pow($longVal, $otherVal));
   }
}

?>