首頁  >  文章  >  後端開發  >  PHP 中的平方根

PHP 中的平方根

WBOY
WBOY原創
2024-08-29 13:12:46295瀏覽

計算其他根,例如數字的 n 次方根或數字的立方根,類似地,我們需要在 PHP 中求數字的平方根。我們透過使用不同的函數(如 pow()、log() 等)來計算這些根。

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

在 PHP 這樣的程式語言中,與內建函數一起使用時計算平方根很簡單。這個函數就是sqrt()。我們還將了解如何在不使用 sqrt() 的情況下找到數字的平方根,以及如何使用帶有使用者輸入的表單來計算平方根。

sqrt() 函數用來計算給定數字的平方根。此函數是 PHP 中使用的內建數學函數,如 pow()、rand()、is_nan() 等

平方根邏輯

平方根邏輯的語法和描述在下面詳細解釋,

文法:

sqrt($num)

其中 $num 是傳遞給 sqrt 函數的單一參數。

描述:sqrt() 函數計算並傳回給定數字的平方根。傳回值是float類型。此外,我們對給定函數有不同類型的輸入數字,在這些數字上執行平方根函數並計算結果。

這裡我們會看到輸入的數字可以是正數、負數或小數(浮點數),也可以是零。正數回傳正數作為輸出,負數回傳 NAN(非數字)作為輸出,十進制數的平方根是浮點數作為輸出,一的平方根是 1。另外,請記住零的平方根為零。

求給定數的平方根

給定數字的平方根如下,

如果輸入數字是81,則該數字的平方根將為9。如果輸入數字為49,則該數字的平方根將為7,依此類推。

讓我們透過一個例子來學習:

我們也將學習使用不同類型的輸入求平方根。

範例#1

代碼:

<?php
// simple example to find how sqrt() function works on numbers
echo sqrt(16);
echo '<br>';
// output is 4
echo sqrt(7);
echo '<br>';
//output is 2.6457513110646
?>

輸出:

PHP 中的平方根

在上面的程式中,輸出是 4,我們知道 4*4 是 16,因此 16 的平方根是 4。在計算 7 的平方根時,我們看到小數點後面有很多位,數字是小數點後的位數取決於使用者。

類似 sqrt 函數,計算給定數字的平方根。為了計算給定數字的任意根,我們使用 pow() 函數,它代表冪。

範例#2

代碼 :

<?php
// example to calculate any root
echo '<br>'.'Result of  :   pow(16, 1/2)  ======  '. pow(16, 1/2);
// example to calculate the cube root of 27
echo '<br>'.'Result of  : pow(27, 1/3)  ======  '. pow(27, 1/3);
//example to calculate the fourth root of 12
echo '<br>'.'Result of  : pow(12, 1/4)  ======  '. pow(12, 1/4);
//example to calculate the fifth root of 76
echo '<br>'.'Result of  : pow(76, 1/5)  ======  '. pow(76, 1/5);
//example to calculate the sixth root of 88
echo '<br>'.'Result of  : pow(88, 1/6)  ======  '. pow(88, 1/6);
?>

輸出:

PHP 中的平方根

範例#3

代碼:

<?php
echo '<br>'.'Result of  :   sqrt(625)  ======  '. sqrt(625);
echo '<br>'.'Result of  :   sqrt(49)  ======  '. sqrt(49);
echo '<br>'.'Result of  :   sqrt(-36)  ======  '. sqrt(-36);
echo '<br>'.'Result of  :   sqrt(0)  ======  '. sqrt(0);
echo '<br>'.'Result of  :   sqrt(121)  ======  '. sqrt(121);
echo '<br>'.'Result of  :   sqrt(22)  ======  '. sqrt(22);
echo '<br>'.'Result of  :   sqrt(12.34)  ======  '. sqrt(12.34);
echo '<br>'.'Result of  :   sqrt(-16)  ======  '. sqrt(-16);
?>

輸出:

PHP 中的平方根

範例#4

求使用者透過表單輸入的數字的平方根:在下面的程式中,我們用 PHP 建立了一個程式來計算使用者透過表單輸入的數字的平方根。假設使用者輸入了 16,那麼我們可以找到 16 的平方根,並期望結果為 4,如果使用者輸入 49,我們可以期望結果為 7,依此類推。

此外,我們也使用內建數學函數 sqrt() 來求平方根。

代碼:

<!---program to calculate square root of input number using form -->
<html>
<head>
<title>Square root of a number using form</title>
</head>
<body>
<!--- input form with text box --->
<form method="post" action="">
<label>Enter a number</label>
<input type="text" name="input" value="" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['submit'])) {
//storing the number in a variable $input
$input = $_POST['input'];
//storing the square root of the number in a variable $ans
$ans = sqrt($input);
//printing the result
echo 'The square root of '.$input.'====='.$ans;
}
?>
</body>
</html>

輸出 – 1:

PHP 中的平方根

輸出 – 2:輸入 100。

PHP 中的平方根

範例#5

不使用內建sqrt() 函數求數字的平方根:在下面的程式中,我們在PHP 中建立了一個程式來計算數字的平方根,而不使用內建函數sqrt() 函數。

代碼:

function squareroot($input)
{
//if the input number is 0 then return 0 as result
if($input == 0) {
return 0;
}
//if the input number is 1 then return 1 as result
if($input == 1) {
return 1;
}
// assigning $input value to a variable $a
$a = $input;
$b = 1;
while($a > $b)
{
// calculating the middle number
$a= ($a + $b)/2;
// dividing the input number with the middle number
$b = $input/$a;
}
return $a;
}
echo '<br>'.'Square root of 0 is '.squareroot(0);
echo '<br>'.'Square root of 20 is '.squareroot(20);
echo '<br>'.'Square root of 49 is '.squareroot(49);
echo '<br>'.'Square root of 81 is '.squareroot(81);
echo '<br>'.'Square root of 1 is '.squareroot(1);

輸出:

PHP 中的平方根

結論

在本文中,我們學習了什麼是平方根,以及如何使用和不使用 sqrt()、pow() 等內建函數來計算平方根。 sqrt() 和 pow() 函數的作用是什麼?如何在程式中使用它來求平方根?我們學習如何將數字、浮點數、負數等平方根。我們也學習如何使用表單透過使用者定義的輸入來計算平方根。

以上是PHP 中的平方根的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn