note 38802 added to function.hypot
| From: | robinv at ecosse dot net | Date: | Wed, 07 Jan 2004 14:18:26 +0000 |
| Subject: | note 38802 added to function.hypot | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-63069@lists.php.net to get a copy of this message | ||
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.
?>
----
Manual Page -- http://www.php.net/manual/en/function.hypot.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+38802
Delete -- http://master.php.net/manage/user-notes.php?action=delete+38802&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+38802&report=yes
Search -- http://master.php.net/manage/user-notes.php