|  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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: a at b dot c dot de
New email:
PHP Version: OS:


 [2010-08-30 06:50 UTC] a at b dot c dot de
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:

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


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-08-31 10:15 UTC]
-Status: Open +Status: Wont fix
 [2010-08-31 10:15 UTC]
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-2021 The PHP Group
All rights reserved.
Last updated: Tue Apr 20 07:02:18 2021 UTC