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
Release: 2023-08-30 14:46:01
forward
1067 people have browsed it

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)
Copy after login

Parameters

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

  • $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.

Return value

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

< h2>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;
?>
Copy after login

Output

Output without scale value: 4
Copy after login

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;
?>
Copy after login

Output

Output with scale value: 4.690
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template