sqrt()函數傳回一個正浮點數的平方根。由於負數的平方根未定義,它會傳回NAN。這是最常用的函數之一。
此函數總是傳回一個浮點數。
sqrt ( float $arg ) : float
#序號 | #參數和描述 |
---|---|
要求平方根的數字
傳回值PHP sqrt() 函數傳回給定arg 數字的平方根。對於負數,函數傳回 NAN。 PHP 版本這個函數在 PHP 4.x、PHP 5.x 和 PHP 7.x 版本都可用。 範例 線上示範以下範例計算100 的平方根−
<?php $arg = 100; echo "Square root of " . $arg . "=" . sqrt($arg) . "</p><p>"; ?>
Square root of 100=10
<?php echo "sqrt(2) = " . sqrt(2) . "</p><p>"; echo "M_SQRT2 = " . M_SQRT2. "</p><p>"; echo "sqrt(3) = " . sqrt(3) . "</p><p>"; echo "M_SQRT3 = " . M_SQRT3 . "</p><p>"; echo "1/sqrt(2)) = " . 1/sqrt(2) . "</p><p>"; echo "M_SQRT1_2 = " . M_SQRT1_2 . "</p><p>"; ?>
sqrt(2) = 1.4142135623731 M_SQRT2 = 1.4142135623731 sqrt(3) = 1.7320508075689 M_SQRT3 = 1.7320508075689 1/sqrt(2)) = 0.70710678118655 M_SQRT1_2 = 0.70710678118655
<?php echo "sqrt(pi) = " . sqrt(M_PI) . "</p><p>"; echo "M_SQRTPI = " . M_SQRTPI. "</p><p>"; echo "2/sqrt(pi) = " . 2/sqrt(M_PI) . "</p><p>"; echo "M_2_SQRTPI = " . M_2_SQRTPI . "</p><p>"; ?>
sqrt(pi) = 1.7724538509055 M_SQRTPI = 1.7724538509055 2/sqrt(pi) = 1.1283791670955 M_2_SQRTPI = 1.1283791670955
<?php echo "sqrt(-1) = " . sqrt(-1) . "</p><p>"; ?>
sqrt(-1) = NAN
以上是PHP sqrt() 函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!