PHP Velho Oeste 2024

bcsqrt

(PHP 4, PHP 5, PHP 7, PHP 8)

bcsqrt任意精度数字的二次方根

说明

bcsqrt(string $num, ?int $scale = null): string

返回 num 的二次方根。

参数

num

操作数,格式良好的 BCMath 数字字符串。

scale

此可选参数用于设置结果中小数点后的小数位数。也可通过使用 bcscale() 来设置全局默认的小数位数,用于所有函数。如果未设置,则默认为 0

返回值

返回平方根,作为格式良好的 BCMath 数字字符串。

错误/异常

此函数在以下情况下引发 ValueError 错误:

  • num 不是格式良好的 BCMath 数字字符串
  • num 小于 0
  • scale 超出有效范围

更新日志

版本 说明
8.0.0 如果 num 不是格式良好的 BCMath 数字字符串,或小于 0,则会引发 ValueError 错误。之前,会引发 E_WARNING 错误。
8.0.0 现在,scale 的取值范围必须在 02147483647 之间;之前,负数的 scale 值会被静默处理为 0
8.0.0 现在 scale 可以为 null。

示例

示例 #1 bcsqrt() 示例

<?php

echo bcsqrt('2', 3); // 1.414

?>

参见

  • bcpow() - 任意精度数字的乘方

add a note add a note

User Contributed Notes 1 note

up
1
markogrady18 at gmail dot com
9 years ago
The bcsqrt function is very handy for finding the square root of numbers in the form of strings.

//EXAMPLE:
   
<?php

$arr
= array(
       
"one" => "20",
        
"two" => "12"
   
);

echo
bcsqrt($arr["one"], 3);

//OUTPUT: 4.472
To Top