PHP Doku:: Quadratwurzel - function.sqrt.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzMathematische ErweiterungenMathematische FunktionenMathematische Funktionensqrt

Ein Service von Reinhard Neidl - Webprogrammierung.

Mathematische Funktionen

<<sinh

srand>>

sqrt

(PHP 4, PHP 5)

sqrtQuadratwurzel

Beschreibung

float sqrt ( float $arg )

Berechnet die Quadratwurzel des Parameters arg.

Parameter-Liste

arg

Der zu verarbeitende Wert

Rückgabewerte

Die Quadratwurzel des Parameters arg oder der spezielle Wert NAN für negative Eingabewerte.

Beispiele

Beispiel #1 sqrt() Beispiel

<?php
// Die Anzahl der Nachkommastellen hängt von der 'precision' Einstellung ab
echo sqrt(9); // 3
echo sqrt(10); // 3.16227766 ...
?>

Siehe auch


11 BenutzerBeiträge:
- Beiträge aktualisieren...
wietse89 at gmail dot com
27.11.2009 21: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
?>
netmask at gmx dot de
17.08.2009 11:45
My little function to get the x'th square root of a value

<?php
/*
* Example: second square root of 256 = 8 [reverse: pow(2,8) = 256]
*/
function xsqrt($x, $value)
{
   
$count = 0;
    do{
       
$value=$value/$x;
       
$count++;
    }while(
$value>=$x);

    return
$count;
}

/* Example ip subnet: */

$net_size = 256;
$subnet = 32 - xsqrt(2, $net_size);
echo
'/' . $subnet;

/* returns: /24
*/
?>

29.03.2006 22: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
"<br>perfect_square_root is of $s is $n";
}

?>
php NOSPAM at primezero dot com
13.07.2005 16: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 "<br>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.05.2005 17: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.03.2005 12: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.02.2005 20: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.05.2004 21: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.07.2003 7: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.11.2002 21: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
5.11.2001 1: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.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)