note 35803 added to function.hypot
| From: | mauddib at rn2 dot php dot net | Date: | Mon, 15 Sep 2003 12:32:36 +0000 |
| Subject: | note 35803 added to function.hypot | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-56521@lists.php.net to get a copy of this message | ||
Too bad this function is not made generic:
<?php
/* Precondition: first and second argument are coordinates
in the same euclidian space.
Postcondition: function return value is equal to the
distance of the first and the second vector or -1 if the
two arguments are not in the same euclidian space.
*/
function gen_hypot(&$x, &$y) {
/* check number of coordinates */
if (count($x) != count($y)) {
/* no match, return an impossible value, -1 */
$r = -1;
} else {
for ($i = 0; $i < count($x); $i++) {
$s = $x[$i] - $y[$i];
$r += $s * $s;
}
}
return sqrt($r);
}
/* example call */
print gen_hypot(array(0, 0, 2), array(2, 3, 8)); /* output: 7 */
?>
like this. This will work for any euclidian coordinate system, not only for R^2.
----
Manual Page -- http://www.php.net/manual/en/function.hypot.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35803
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35803&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35803&report=yes