PHP Doku:: Länge der Hypotenuse eines rechtwinkligen Dreiecks - function.hypot.html

Verlauf / Chronik / History: (1) anzeigen

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

Ein Service von Reinhard Neidl - Webprogrammierung.

Mathematische Funktionen

<<hexdec

is_finite>>

hypot

(PHP 4 >= 4.1.0, PHP 5)

hypot Länge der Hypotenuse eines rechtwinkligen Dreiecks

Beschreibung

float hypot ( float $x , float $y )

hypot() berechnet die Länge der Hypotenuse eines rechtwinkligen Dreiecks aus den Längen der beiden Katheten bzw. den Abstand eines Punktes (x,y) vom Ursprung. Dies entspricht sqrt(x*x + y*y).

Parameter-Liste

x

Länge der ersten Kathete

y

Länge der zweiten Kathete

Rückgabewerte

Berechnete Länge der Hypotenuse


3 BenutzerBeiträge:
- Beiträge aktualisieren...
.org
10.08.2006 11:56
to robinv at ecosse dot net:

hypo(a, b, c) === hypo(a, hypo(b, c))
hypo(a, b, c, d) === hypo(a, hypo(b, hypo(c, d)))
...
R. Victor Klassen
24.06.2005 17:03
A correct implementation of hypot( x, y ) avoids the overflow that might otherwise happen if either x or y is large enough that when squared it would overflow, but the answer is small enough not to cause overflow.
robinv at ecosse dot net
7.01.2004 18:18
A simpler approach would be to allow an arbitrary number of parameters. That would allow for whatever number of dimensions you want *and* it would be backwards compatible with the current implementation.

<?php

function hypo()
{
   
$sum = 0;
    foreach (
func_get_args() as $dimension) {
        if (!
is_numeric($dimension)) return -1;
       
$sum += pow($dimension, 2);
    }
    return
sqrt($sum);
}

print
hypo();          // vector in 0 dimensions, magnitude = 0.
print hypo(1);         // vector in 1 dimension,  magnitude = 1.
print hypo(3, 4);       // vector in 2 dimensions, magnitude = 5.
print hypo(2, 3, 6);     // vector in 3 dimensions, magnitude = 7.

?>



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",...)