hypot

(PHP 4 >= 4.0.7, PHP 5)

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

Описание

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

Список параметров

x

Length of first side

y

Length of second side

Возвращаемые значения

Calculated length of the hypotenuse

Коментарии

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($dimension2);
    }
    return 
sqrt($sum);
}

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

?>
2004-01-07 11:18:26
http://php5.kiev.ua/manual/ru/function.hypot.html
Автор:
If you need a higher-dimensional diagonal length (norm), you can exploit the fact that sqrt(x*x+y*y+z*z) == sqrt(x*x+sqrt(y*y+z*z)). In other words hypot(x, y, z)==hypot(x, hypot(y, z)).

To be generic about it....
<?php

function norm(...$args)
{
    return 
array_reduce($args'hypot'0);
}

?>
2017-09-04 14:17:55
http://php5.kiev.ua/manual/ru/function.hypot.html

    Поддержать сайт на родительском проекте КГБ