sqrt


php128 apg

RETURNS a number as square root of another number.

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





This function returns the square root of $num if $num ≥ 0.

This function returns a special value NAN, if $num < 0.

Where NAN = Not A Number.



<?php

float sqrt 
( float $num )


where,

$num = The argument to extract the square root

?>
 

$num


The argument to extract the square root.



  1 EXERCISE   

<?php

setlocale
(LC_ALL, 'pt-PT', 'pt_PT');

$arg01a = 2;

$arg01b = '2';

$arg01c = M_PI;

echo 
'sqrt( ' . $arg01a . ' ) = ' . sqrt($arg01a) . '<br><br>';

echo 
'sqrt( ' . $arg01b . ' ) = ' . sqrt($arg01b) . '<br><br>';

echo 
'sqrt( ' . $arg01c . ' ) = ' . sqrt($arg01c) . '<br>';

?> 

  2 EXERCISE   

<?php

setlocale
(LC_ALL, 'pt-PT', 'pt_PT');

$arg02 = -M_E;

echo 
sqrt($arg02);

?>

  3 EXERCISE   

<?php

setlocale
(LC_ALL, 'pt-PT', 'pt_PT'); 

echo 
"Testing sqrt() : usage variations.<br><br><br>";

$values = [ 23,
        -
23,
        
2.345e1,
        -
2.345e1,
        
0x17,
        
027,
        
"23",
        
"23.45",
        
"2.345e1",
        
"1000",
        
null,
        
true,
        
false ];

for (
$i = 0; $i < count($values); $i++) {
    
$res = sqrt($values[$i]);
    
var_dump($res);
    echo 
'<br><br>';
}

?>

  4 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
);


foreach (
$longVals as $longVal) {
   echo 
"<br><br>Testing: $longVal<br>";
   
var_dump(sqrt($longVal));
}

?>