Home >Backend Development >PHP Tutorial >PHP - How to get the square root of an arbitrary precision number using the bcsqrt() function?

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

WBOY
WBOYforward
2023-08-30 14:45:021063browse

PHP - 如何使用bcsqrt()函数获取任意精度数字的平方根?

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

Syntax

string bcsqrt($num_string, $scale)

Parameters

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

  • $num_string - It represents the number whose square root is to be calculated. String type parameter.

  • $scale − represents the number of digits appearing after the decimal point in the output. p>

Return value

bcsqrt()The function returns the square root of the number in string form.

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

The above is the detailed content of PHP - How to get the square root of an arbitrary precision number using the bcsqrt() function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete