首頁  >  文章  >  後端開發  >  PHP - 如何使用bcsqrt()函數取得任意精確度數字的平方根?

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

WBOY
WBOY轉載
2023-08-30 14:45:02971瀏覽

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

在 PHP 中,bcsqrt() 函數用來取得任意精確度數字的平方根。它接受任意精度數字作為字串,並在將結果縮放到指定精度後給出該數字的平方根。

語法

string bcsqrt($num_string, $scale)

參數

bcsqrt()函數接受兩個不同的參數:$num_string $scale。 p>

  • $num_string - 它表示要計算其平方根的數字。字串類型參數。

  • $scale − 表示輸出中小數點後出現的位數。 p>

傳回值

bcsqrt()函數以字串形式傳回數字的平方根。

範例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中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除