dismiss Step into the future! Click here to switch to the beta php.net site
downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

is_finite> <hexdec
[edit] Last updated: Fri, 28 Jun 2013

view this page in

hypot

(PHP 4 >= 4.1.0, PHP 5)

hypot Calculate the length of the hypotenuse of a right-angle triangle

Description

float hypot ( float $x , float $y )

hypot() returns the length of the hypotenuse of a right-angle triangle with sides of length x and y, or the distance of the point (x, y) from the origin. This is equivalent to sqrt(x*x + y*y).

Parameters

x

Length of first side

y

Length of second side

Return Values

Calculated length of the hypotenuse



is_finite> <hexdec
[edit] Last updated: Fri, 28 Jun 2013
 
add a note add a note User Contributed Notes hypot - [3 notes]
up
1
robinv at ecosse dot net
9 years ago
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.

?>
up
0
R. Victor Klassen
8 years ago
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.
up
-1
</Life>.org
6 years ago
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)))
...

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