International PHP Conference 2015

# hypot

(PHP 4 >= 4.1.0, PHP 5)

hypot Calcula o tamanho da hipotenusa de um ângulo reto do triângulo Retorna a raiz quadrada de (num1*num1 + num2*num2)

### Descrição

float hypot ( float \$x , float \$y )

hypot() retorna o tamanho da hipotenusa de um ângulo reto do triângulo com lado de tamanho x e y, ou a distância do ponto (x, y) de uma origem. O equivalente a sqrt(x*x + y*y).

x

y

### User Contributed Notes 1 note

robinv at ecosse dot net
11 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.

?>