PHP | bcsqrt()函数

PHP中的bcsqrt()函数是一个内置函数,用于计算任意精度数字的平方根。此函数接受任意精度的数字作为字符串,并在将结果缩放到指定精度后返回该数字的平方根。

null

语法:

string bcsqrt ( $num_str, $scaleVal)

参数: 此函数接受三个参数,如上面的语法所示,如下所述:

  • $num_str :此参数为字符串类型,表示要计算其平方根的操作数或数字。此参数是必需的。
  • $scaleVal :此参数为int类型,是可选的。此参数表示结果中小数点后出现的位数。它的默认值是零。

返回值: 此函数用于返回数字的平方根 $num_str 就像绳子一样。

例如:

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

以下程序说明了PHP中的bcsqrt()函数:

项目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 ;
?>


输出:

5

项目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 ;
?>


输出:

5.0990

参考: http://php.net/manual/en/function.bcsqrt.php

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享