Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Mathematical Functions : hypot

hypot

Calculate the length of the hypotenuse of a right-angle triangle (PHP 4 >= 4.0.7, PHP 5)
float hypot ( float x, float y )


Code Examples / Notes » hypot

.org

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


robinv

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


r. victor klassen

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.

Change Language


Follow Navioo On Twitter
abs
acos
acosh
asin
asinh
atan2
atan
atanh
base_convert
bindec
ceil
cos
cosh
decbin
dechex
decoct
deg2rad
exp
expm1
floor
fmod
getrandmax
hexdec
hypot
is_finite
is_infinite
is_nan
lcg_value
log10
log1p
log
max
min
mt_getrandmax
mt_rand
mt_srand
octdec
pi
pow
rad2deg
rand
round
sin
sinh
sqrt
srand
tan
tanh
eXTReMe Tracker