php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52734 Extend hypot() to three dimensions
Submitted: 2010-08-30 06:50 UTC Modified: 2010-08-31 10:15 UTC
From: a at b dot c dot de Assigned:
Status: Wont fix Package: Math related
PHP Version: 5.3.3 OS: Windows XP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
13 - 5 = ?
Subscribe to this entry?

 
 [2010-08-30 06:50 UTC] a at b dot c dot de
Description:
------------
The existing hypot() function is limited to finding the hypotenuse of a right-angled triangle in two dimensions ($h = hypot($x,$y)), when in fact it is well-defined for higher dimensions as well as the length of the diagonal of a (hyper)cuboid with edge lengths $x,$y,$z...; it's the square root of the sum of the squares of the edge lengths.

For geometric purposes it would be helpful to have at least hypot($x,$y,$z). The existing alternatives are sqrt($x*$x+$y*$y+$z*$z) or hypot(hypot($x,$y),$z). The latter is (not surprisingly) markedly slower, and also less accurate.

Test script:
---------------
function length($v1, $v2)
{
    return hypot($v1[0]-$v2[0], $v1[1]-$v2[1], $v1[2]-$v2[2]);
}

$v1 = array(1, 1, 1);
$v2 = array(5, 6, 1);
echo length($v1, $v2);


Expected result:
----------------
6.4031242374328

Actual result:
--------------
A warning about hypot expecting exactly two parameters, and a NULL return value.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-08-31 10:15 UTC] aharvey@php.net
-Status: Open +Status: Wont fix
 [2010-08-31 10:15 UTC] aharvey@php.net
hypot() was intended to be (and is generally implemented as) a wrapper for the C99 and POSIX hypot() function, which itself only accepts two dimensions. Given that this can be easily implemented in PHP itself using sqrt() (as you demonstrate), I don't see any real benefit in diverging from the underlying implementation.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 19:01:28 2024 UTC