Open In App

PHP | bcsqrt() Function

The bcsqrt() function in PHP is an inbuilt function and is used to evaluate the square root of an arbitrary precision number. This function accepts an arbitrary precision number as a string and returns the square root of the number after scaling the result to a specified precision.

Syntax:



string bcsqrt ( $num_str, $scaleVal)

Parameters: This function accepts three parameters as shown in the above syntax and explained below:

Return Value: This function returns the square root of a number $num_str as string.



Examples:

Input:  $num_str = 26
Output: 5
Since the parameter $scaleVal is not specified so
no digits after decimal is appeared in the 
result after finding out square root. 

Input:  $num_str = 26, $scaleVal = 4
Output: 5.0990

Below programs illustrate the bcsqrt() function in PHP :

Program 1:




<?php
// PHP program to illustrate bcsqrt() function
   
// input numbers with arbitrary precision
$num_str = "26";
   
// calculates the square root when 
// $scaleVal is not specified
$res = bcsqrt($num_str);
  
echo $res;
   
?>

Output:

5

Program 2:




<?php
// PHP program to illustrate bcsqrt() function
   
// input numbers with arbitrary precision
$num_str = "26";
$scale = "4";
   
// calculates the square root when 
// $scaleVal is specified
$res = bcsqrt($num_str, $scale);
  
echo $res;
   
?>

Output:

5.0990

Reference:
http://php.net/manual/en/function.bcsqrt.php


Article Tags :