PHP – How to get the square root of an arbitrary precision number using bcsqrt() function?

PHPServer Side ProgrammingProgramming

In PHP, the bcsqrt() function is used to get the square root of an arbitrary precision number. It accepts the arbitrary precision number as a string and gives the square root of the number after scaling the result to the specified precision.

Syntax

string bcsqrt($num_string, $scale)

Parameters

The bcsqrt() function accepts two different parameters: $num_string and $scale.

  • $num_string − It represents the number whose square root to be evaluated. It is a string-type parameter.

  • $scale − It indicates the number of digits that appear after the decimal point in the output.

Return Value

The bcsqrt() function returns the square root of the number as a string.

Example 1

<?php
   // input numbers with arbitrary precision
   $num_string = "22";

   // below bcsqrt function calculates the square root
   $result= bcsqrt($num_string);
   echo "Output without scale value: ", $result;
?>

Output

Output without scale value: 4

Example 2

<?php
   // input numbers with arbitrary precision
   $num_string = "22";

   //scale value 3
   $scale="3";

   // below bcsqrt function calculates the square root
   $result= bcsqrt($num_string, $scale);
   echo "Output with scale value: ", $result;
?>

Output

Output with scale value: 4.690
raja
Published on 21-Aug-2021 07:38:48
Advertisements