Skip to content

Commit f588ab5

Browse files
committed
The purpose of hypot() isn't to have a pythagoras shortcut, but to overcome overflow issues.
1 parent e6a3d00 commit f588ab5

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

functions/math/hypot.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
function hypot(x, y) {
22
// discuss at: http://phpjs.org/functions/hypot/
33
// original by: Onno Marsman
4+
// imprived by: Robert Eisele (http://www.xarg.org/)
45
// example 1: hypot(3, 4);
56
// returns 1: 5
67
// example 2: hypot([], 'a');
78
// returns 2: 0
89

9-
return Math.sqrt(x * x + y * y) || 0;
10+
x = Math.abs(x);
11+
y = Math.abs(y);
12+
13+
var t = Math.min(x, y);
14+
x = Math.max(x, y);
15+
t = t / x;
16+
17+
return x * Math.sqrt(1 + t * t);
1018
}

0 commit comments

Comments
 (0)