計(jì)算其他根,例如數(shù)字的 n 次方根或數(shù)字的立方根,類(lèi)似地,我們需要在 PHP 中求數(shù)字的平方根。我們透過(guò)使用不同的函數(shù)(如 pow()、log() 等)來(lái)計(jì)算這些根。
廣告
該類(lèi)別中的熱門(mén)課程
PHP 開(kāi)發(fā)人員 - 專(zhuān)業(yè)化 | 8 門(mén)課程系列 | 3次模擬測(cè)驗(yàn)
開(kāi)始您的免費(fèi)軟體開(kāi)發(fā)課程
網(wǎng)頁(yè)開(kāi)發(fā)、程式語(yǔ)言、軟體測(cè)試及其他
在 PHP 這樣的程式語(yǔ)言中,與內(nèi)建函數(shù)一起使用時(shí)計(jì)算平方根很簡(jiǎn)單。這個(gè)函數(shù)就是sqrt()。我們還將了解如何在不使用 sqrt() 的情況下找到數(shù)字的平方根,以及如何使用帶有使用者輸入的表單來(lái)計(jì)算平方根。
sqrt() 函數(shù)用來(lái)計(jì)算給定數(shù)字的平方根。此函數(shù)是 PHP 中使用的內(nèi)建數(shù)學(xué)函數(shù),如 pow()、rand()、is_nan() 等
平方根邏輯
平方根邏輯的語(yǔ)法和描述在下面詳細(xì)解釋?zhuān)?/p>
文法:
sqrt($num)
其中 $num 是傳遞給 sqrt 函數(shù)的單一參數(shù)。
描述:sqrt() 函數(shù)計(jì)算並傳回給定數(shù)字的平方根。傳回值是float類(lèi)型。此外,我們對(duì)給定函數(shù)有不同類(lèi)型的輸入數(shù)字,在這些數(shù)字上執(zhí)行平方根函數(shù)並計(jì)算結(jié)果。
這裡我們會(huì)看到輸入的數(shù)字可以是正數(shù)、負(fù)數(shù)或小數(shù)(浮點(diǎn)數(shù)),也可以是零。正數(shù)回傳正數(shù)作為輸出,負(fù)數(shù)回傳 NAN(非數(shù)字)作為輸出,十進(jìn)制數(shù)的平方根是浮點(diǎn)數(shù)作為輸出,一的平方根是 1。另外,請(qǐng)記住零的平方根為零。
求給定數(shù)的平方根
給定數(shù)字的平方根如下,
如果輸入數(shù)字是81,則該數(shù)字的平方根將為9。如果輸入數(shù)字為49,則該數(shù)字的平方根將為7,依此類(lèi)推。
讓我們透過(guò)一個(gè)例子來(lái)學(xué)習(xí):
我們也將學(xué)習(xí)使用不同類(lèi)型的輸入求平方根。
範(fàn)例#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
?>
輸出:

在上面的程式中,輸出是 4,我們知道 4*4 是 16,因此 16 的平方根是 4。在計(jì)算 7 的平方根時(shí),我們看到小數(shù)點(diǎn)後面有很多位,數(shù)字是小數(shù)點(diǎn)後的位數(shù)取決於使用者。
類(lèi)似 sqrt 函數(shù),計(jì)算給定數(shù)字的平方根。為了計(jì)算給定數(shù)字的任意根,我們使用 pow() 函數(shù),它代表冪。
範(fàn)例#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);
?>
輸出:

範(fàn)例#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);
?>
輸出:

範(fàn)例#4
求使用者透過(guò)表單輸入的數(shù)字的平方根:在下面的程式中,我們用 PHP 建立了一個(gè)程式來(lái)計(jì)算使用者透過(guò)表單輸入的數(shù)字的平方根。假設(shè)使用者輸入了 16,那麼我們可以找到 16 的平方根,並期望結(jié)果為 4,如果使用者輸入 49,我們可以期望結(jié)果為 7,依此類(lèi)推。
此外,我們也使用內(nèi)建數(shù)學(xué)函數(shù) sqrt() 來(lái)求平方根。
代碼:
<!---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:

輸出 – 2:輸入 100。

範(fàn)例#5
不使用內(nèi)建sqrt() 函數(shù)求數(shù)字的平方根:在下面的程式中,我們?cè)赑HP 中建立了一個(gè)程式來(lái)計(jì)算數(shù)字的平方根,而不使用內(nèi)建函數(shù)sqrt() 函數(shù)。
代碼:
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);
輸出:

結(jié)論
在本文中,我們學(xué)習(xí)了什麼是平方根,以及如何使用和不使用 sqrt()、pow() 等內(nèi)建函數(shù)來(lái)計(jì)算平方根。 sqrt() 和 pow() 函數(shù)的作用是什麼?如何在程式中使用它來(lái)求平方根?我們學(xué)習(xí)如何將數(shù)字、浮點(diǎn)數(shù)、負(fù)數(shù)等平方根。我們也學(xué)習(xí)如何使用表單透過(guò)使用者定義的輸入來(lái)計(jì)算平方根。
以上是PHP 中的平方根的詳細(xì)內(nèi)容。更多資訊請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!