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


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

語法

string bcsqrt($num_string, $scale)

引數

**bcsqrt()** 函式接受兩個不同的引數:**$num_string** 和 **$scale**。

  • **$num_string −** 它表示要計算平方根的數字。這是一個字串型別的引數。

  • **$scale −** 它指示輸出中小數點後出現的數字位數。

返回值

**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

更新於: 2021年8月21日

瀏覽量 153 次

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.