Tutorialspoint.dev

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:

  • $num_str: This parameter is of string type and represents the operand or a number whose square root is to be evaluated. This parameter is mandatory.
  • $scaleVal: This parameter is of int type and is optional. This parameter tells the number of digits that will appear after the decimal in the result. It’s default value is zero.

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



This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter