數(shù)値の n 乗根や數(shù)値の立方根など、他の根を計(jì)算する場(chǎng)合も同様に、PHP で數(shù)値の平方根を求める必要があります。これらのルートは、pow()、log() などのさまざまな関數(shù)を使用して計(jì)算します。
広告
このカテゴリーの人気コース
PHP 開(kāi)発者 - 専門(mén)分野 | 8コースシリーズ | 3 つの模擬テスト
無(wú)料ソフトウェア開(kāi)発コースを始めましょう
Web 開(kāi)発、プログラミング言語(yǔ)、ソフトウェア テスト、その他
PHP のようなプログラミング言語(yǔ)では、組み込み関數(shù)を使用すると平方根の計(jì)算が簡(jiǎn)単になります。この関數(shù)は sqrt() です。また、sqrt() を使用せずに數(shù)値の平方根を求める方法と、ユーザー入力フォームを使用して平方根を計(jì)算する方法についても説明します。
sqrt() 関數(shù)は、指定された數(shù)値の平方根を計(jì)算するために使用されます。この関數(shù)は、pow()、rand()、is_nan() などの PHP で使用される組み込みの數(shù)學(xué)関數(shù)です。
平方根ロジック
平方根ロジックの構(gòu)文と説明については、以下で詳しく説明します。
構(gòu)文:
sqrt($num)
ここで、$num は sqrt 関數(shù)に渡される?yún)g一の引數(shù)です。
説明: sqrt() 関數(shù)は、指定された數(shù)値の平方根を計(jì)算して返します。戻り値の型は float です。また、平方根関數(shù)が実行され、結(jié)果が計(jì)算される特定の関數(shù)には、さまざまな種類の入力數(shù)値があります。
ここでは、入力數(shù)値が正または負(fù)の數(shù)値、または 10 進(jìn)數(shù) (浮動(dòng)小數(shù)點(diǎn)數(shù)) であることも、ゼロであることもあることがわかります。正の數(shù)値は出力として正の數(shù)値を返し、負(fù)の數(shù)値は出力として NAN (Not a Number) を返します。10 進(jìn)數(shù)の平方根は出力として浮動(dòng)小數(shù)點(diǎn)であり、1 の平方根は 1 です。また、ゼロの平方根はゼロであることを覚えておいてください。
指定された數(shù)値の平方根を求める
指定された數(shù)値の平方根は次のとおりです。
入力數(shù)値が 81 の場(chǎng)合、數(shù)値の平方根は 9 になります。入力數(shù)値が 49 の場(chǎng)合、平方根數(shù)値は 7 になります。
例でこれを?qū)Wびましょう:
さまざまなタイプの入力で平方根を求める方法も學(xué)びます。
例 #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 が 16 であることがわかっているため、出力は 4 です。したがって、16 の平方根は 4 です。7 の平方根を計(jì)算しているときに、10 進(jìn)數(shù)の多くの桁が見(jiàn)つかった後、數(shù)値が計(jì)算されることがわかります。小數(shù)點(diǎn)以下の桁數(shù)はユーザーによって異なります。
指定された數(shù)値の平方根を計(jì)算する sqrt 関數(shù)に似ています。指定された數(shù)値の根を計(jì)算するには、べき乗を表す pow() 関數(shù)を使用します。
例 #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);
?>
出力:

例 #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);
?>
出力:

例 #4
ユーザーがフォームに入力した數(shù)値の平方根を求める: 次のプログラムでは、ユーザーがフォームに入力した數(shù)値の平方根を計(jì)算するプログラムを PHP で作成しました。 。ユーザーが 16 を入力したとすると、16 の平方根を求めて結(jié)果が 4 になることが期待できます。ユーザーが 49 を入力した場(chǎng)合、結(jié)果は 7 になることが期待できます。
また、平方根を求めるために組み込みの數(shù)學(xué)関數(shù) 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:

出力 – 2: 入力として 100 を使用します。

例 #5
組み込みの sqrt() 関數(shù)を使用せずに數(shù)値の平方根を求める: 次のプログラムでは、組み込みの sqrt() 関數(shù)を使用せずに數(shù)値の平方根を計(jì)算するプログラムを PHP で作成しました。 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é)論
この記事では、平方根とは何か、sqrt()、pow() などの組み込み関數(shù)を使用した場(chǎng)合と使用しない場(chǎng)合の平方根の計(jì)算方法について學(xué)びました。 sqrt() 関數(shù)と pow() 関數(shù)は何をするのでしょうか。プログラム內(nèi)で平方根を求めるためにどのように使用されますか?數(shù)値、浮動(dòng)小數(shù)點(diǎn)數(shù)、負(fù)の數(shù)などの平方根の実行について學(xué)びました。また、フォームを使用したユーザー定義入力による平方根の計(jì)算についても學(xué)習(xí)しました。
以上がPHPの平方根の詳細(xì)內(nèi)容です。詳細(xì)については、PHP 中國(guó)語(yǔ) Web サイトの他の関連記事を參照してください。