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
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP