downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

srand> <sinh
[edit] Last updated: Fri, 25 May 2012

view this page in

sqrt

(PHP 4, PHP 5)

sqrt平方根

説明

float sqrt ( float $arg )

arg の平方根を返します。

パラメータ

arg

処理する引数。

返り値

arg の平方根を返します。 負の数を指定した場合は、特別な値 NAN を返します。

例1 sqrt() の例

<?php
// 小数点以下の精度は、precision ディレクティブの設定に依存します
echo sqrt(9); // 3
echo sqrt(10); // 3.16227766 ...
?>

参考



srand> <sinh
[edit] Last updated: Fri, 25 May 2012
 
add a note add a note User Contributed Notes sqrt
Husoski 16-Feb-2012 11:04
This is interesting.  The "xth square root" idea is being used by a couple different posters, but it's not a root at all.  It the logarithm, base x.  In the posted code, it's rounded down to the next smaller or equal integer, but it's still a logarithm.

The full exact value as a float can be computed with log(value)/log(x).  No loops needed.

For true xth roots, use pow(value, 1.0/x).  That gives you the number that produces (value) when raised to the x power.
wietse89 at gmail dot com 27-Nov-2009 12:15
For the second square root this code will work too:
Note that it doesn't return float values.

<?php
$new
= strlen(decbin(256));
echo
$new; //8
?>
btharper1221 at gmail dot com 15-May-2005 08: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)

<?php
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]."<b><i>i</i></b> roots of ".$ret[0];
}elseif(
$ret[0] == 1){
if(
$ret[2] == 0) echo $ret[1];
if(
$ret[2] == 1) echo "$ret[1]<b><i>i</i></b>";
}
?>
chris DOT rutledge AT gmail DOT com 31-Mar-2005 02: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"<b><i>i</i></b>";
jouhni at web dot de 17-Feb-2005 11: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
bishop 17-Jul-2003 10: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.

 
show source | credits | sitemap | contact | advertising | mirror sites