2023-08-30

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

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

在 PHP 中,bcsqrt() 函数用于获取任意精度数字的平方根。它接受任意精度数字作为字符串,并在将结果缩放到指定精度后给出该数字的平方根。

语法

string bcsqrt($num_string, $scale)
登录后复制

参数

bcsqrt()函数接受两个不同的参数:$num_string$scale

  • $num_string – 它表示要计算其平方根的数字。字符串类型参数。

  • $scale − 表示输出中小数点后出现的位数。

返回值

bcsqrt()函数以字符串形式返回数字的平方根。

< h2>示例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 without scale value: 4
登录后复制

示例 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 with scale value: 4.690
登录后复制

以上就是PHP – 如何使用bcsqrt()函数获取任意精度数字的平方根?的详细内容,更多请关注php中文网其它相关文章!

https://www.php.cn/faq/599940.html

发表回复

Your email address will not be published. Required fields are marked *