PHP 3, PHP 4, PHP 5)
sqrt -- 平方根
說明
float sqrt ( float arg )
返回 arg 的平方根。
例 1. sqrt()
// Precision depends on your precision directive
echo sqrt(9); // 3
echo sqrt(10); // 3.16227766 ...
?>
29-Mar-2006 08:03
A much easier way to find perfect squares:
This method uses integer roots to find perfect squares instead of integer squares to find integer roots.
# initial value
$i = 0;
#set any limit you want
$setSomeLimit = 10000;
# do it! .... do it! ... do it!
for ($n = 1; $n < sqrt($setSomeLimit; $n++)
{
// Find the square
$s= $n * $n;
print "
perfect_square_root is of $s is $n";
}
?>
php NOSPAM at primezero dot com
13-Jul-2005 02:15
creating perfect squares... (no decimals)
# initial value
$i = 0;
#set any limit you want
$setSomeLimit = 10000;
# do it! .... do it! ... do it!
do {
# find the square root
$sqRoot = sqrt($i);
# not a decimal? that it is a perfect square! ^_^
if ( ereg ("^[0-9]{1,12}$", $sqRoot)) {
# suppose that the_square_root = teh_square_root
print "
perfect_square_root is of $i is $sqRoot";
}
$i++; # next!!
# keeps running until it reaches the limit
} while ($i < $setSomeLimit);
?>
btharper1221 at gmail dot com
15-May-2005 03:48
this is something you can use if you need an exact answer to a root problem, this includes an echo outside of the function to show how it works, it will return it such as
$ret[0] is the number still part of the root (this is the exactness part of it)
$ret[1] is the number of roots and
$ret[2] is 1 or 0, 1 being that there should be an i following the number of roots (imaginary)
function newrt($val,$rt){
$i = 0;
if(($rt % 2 == 0) && ($val < 1)){
$i = 1;
$val = (-$val);
}
$c = 1;
if(($rt % 2 != 0) && ($val < 0)){
$c = -$c;
$val = -$val;
}
for($d = 2; pow($d,$rt) <= abs($val); $d++){
if($val % (pow($d,$rt)) == 0){
$val = ($val/(pow($d,$rt)));
$c = ($c*$d);
$d--;
}
}
$ret[0] = $val;
$ret[1] = $c;
$ret[2] = $i;
return $ret;
}//end function newrt
$ret = newrt($num,$num2);
if($ret[0] != 1){
if($ret[2] == 0) echo $ret[1]." roots of ".$ret[0];
if($ret[2] == 1) echo $ret[1]."i roots of ".$ret[0];
}elseif($ret[0] == 1){
if($ret[2] == 0) echo $ret[1];
if($ret[2] == 1) echo "$ret[1]i";
}
?>
chris DOT rutledge AT gmail DOT com
31-Mar-2005 10:24
Just a note to say you can take the square root of a negative number - it returns an imaginary number.
One might do it like this
//take initial value of $x
$abs_x = abs($x);
$answer = sqrt($abs_x);
echo $answer;
if ($x < 0) echo"i";
jouhni at web dot de
17-Feb-2005 07:46
To get any root of a number your can use the pow() function:
pow(8, 1/3)
which gives you the third root of eight.
Jouhni
php user
13-May-2004 07:25
How to calculate triangle's area if you haven't height measure.
You need only sides' measure.
it would have to work.
(formula of erone)
$s1 = side 1
$s2 = side 2
$s3 = side 3
function area($s1,$s2,$s3)
{
$calc1 = ($s1+$s2+$s3)/2;
$calc2 = $p*($p-$s1)*($p-$s2)*($p-$s3);
$result = round(sqrt($p),2);
return $result;
}
?>
example of I use:
//This will make to return the area of a triangle with sides of 3,4,5
$area = area(3,4,5);
echo $area;
?>
bishop
18-Jul-2003 05:09
Compute a triangle's area:
function triangleArea($a, $b /* ... */) {
if (func_num_args() === 3) {
$s = .5 * ($a + $b + ($c = func_get_arg(2)));
return sqrt($s * ($s-$a) * ($s-$b) * ($s-$c));
} else {
return (.5 * $a * $b);
}
}
With 2 args, parameters are triangle's base and height. With 3 args, parameters are the lengths of each side. Argument order is not important.
Either way, you get the area.
tcn at pandora dot be
13-Nov-2002 08:37
Checking if the square you want to take from a number is valid or not (you can't take the root of a negative number)
if ($i<0)
{
echo "Error! The number you specified can't be rooted! It's negative!
}
else
{
echo "The square root of your number is ".sqrt($i);
}
morvandiumNOSPAM at yahoo dot com
05-Nov-2001 12:44
I find this useful for any grid system where distances need to be computed (maps and graphs, mostly).
Just use the distance between points formula (pseudo-code): sqrt((x2-x1)^2+(y2-y1)^2) to find the distance between two points.
相關詞條
-
sqrt
sqrt() Precision erlin
-
四次函式
/2+(y-(by-d)/sqrt(8y+b^2-4c))=0就可得到原...方程式的求根公式來解:z={{-a_2\pm\sqrt{a_2... x 的值:x_1=+\sqrt\,x_2=-\sqrt\,x_3...
函式解法 函式圖像 函式情況 -
貝索函式
|自變數的推廣)。第一類貝塞爾函式的形狀大致與按 math 1/\sqrt x...
貝塞爾函式簡介 歷史 現實背景和套用範圍 定義 第一類貝塞爾函式 -
驗證四方定理
mode_1(int n) { if((int)sqrt(n) * (int)sqrt(n) == n){ PRINTF("%d * %d = %d \n",(int)sqrt(n...
-
MAX264
模式下fclk/fo是模式1的{1}\over{\sqrt{2}},這樣...)/\sqrt{2}模式2時; 因為時鐘頻率fclk是已知的,所以即可...: 低通時: f_{c}=f_{0} \sqrt{(1-{1...
MAX264的結構組成與主要特性 MAX264的引腳說明 MAX264原理及設計 -
解平方根
一組數的開平方的最大整數,寫在標記左上方,即 Int(sqrt(最右一組數... 565643826……級數展開⒈由代數式的變換Sqrt(x)=a/b * 1/Sqrt[1-(xb2-a2)/(xb2)]而1/sqrt(1-y) = 1...
一般方法 級數展開 -
量子傳輸
簡介量子傳輸隱形傳輸並不僅僅是科幻小說裡面的故事。它是真實的,並且已經存在了。或者至少,量子的隱形傳輸已經成為了可能:這是指量子...
簡介 概念科普 逾時空穿越 完成過程 理論原理 -
c語言程式
++) { x=sqrt(i+100); /*x為加上100後開方後的結果*/ y=sqrt(i+268); /*y為再加上168後...
-
平方根計算
程式例VC 2008後為重載函式,原型為 float sqrt (float),double sqrt (double),double long sqrt(double long)注意沒有sqrt (int),但是...
程式例 pascal gcc EXCEL函式 Python函式