Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest hypotenuse in javascript?

I've seen a number of questions about simulations and animations in javascript, which often involve calculating the hypotenuse:

hypot = Math.sqrt(x*x + y*y);

Since cartesian coordinates are the weapon of choice in most of these engines, these calculations are needed to find the distance between pairs of points, etc. So any speedup in calculating the hypotenuse could be a great help to many projects.

To that end, can you see a faster method than the simple implementation above? I found an approximation which was marginally faster in Chrome, but turned out to be much slower in Firefox, based on this approximation function in SuperCollider.

Edit 2015-08-15: I've switched the accepted answer to being the Math.hypot one; I suspect the pragmatic approach at present would be to use Math.hypot or a synthesized hypot function if not available, and to compare against the square (per sch's answer) if that is sufficient and Math.hypot is not available.

like image 411
Phil H Avatar asked Apr 13 '12 12:04

Phil H


People also ask

What is math hypot in JavaScript?

The Math. hypot() function in JavaScript is used to calculate the square root of the sum of squares of numbers passed to it as arguments. It is basically used to find the hypotenuse of a right-angled triangle or the magnitude of a complex number.

How do you find the hypotenuse of a right triangle in JavaScript?

Calculating the hypotenuse of a right triangle, or the magnitude of a complex number, uses the formula Math. sqrt(v1*v1 + v2*v2) , where v1 and v2 are the lengths of the triangle's legs, or the complex number's real and complex components.

What is Matlab hypot?

Square root of sum of squares (hypotenuse) - MATLAB hypot.

What is NP hypot?

The np. hypot() is a numpy library function used to calculate the hypotenuse for the right-angled triangle. The numpy hypot() method takes two parameters and returns an array containing values of the hypotenuse of the right-angle triangle.


2 Answers

Often, you don't need to compute the square root and hypot^2 = x*x + y*y is enough. This is the case for example if you want to compare the distances and don't need the actual values.

like image 66
sch Avatar answered Sep 21 '22 13:09

sch


In ECMAScript ES6 you can use Math.hypot:

// ES5 support

Math.hypot = Math.hypot || function(x, y){ return Math.sqrt(x*x + y*y) }

var x = 3, y = 4;

document.write(Math.hypot(x, y))

Edit: You can run this test on a blank tab, are 2 million operations with both methods, the results are very good, it is 24% faster.

var i, tmp, x = 55, y = 66, end, ini = performance.now();

// Math.sqrt operation
i = 0;
ini = performance.now();
tmp = 0;
while(i++ < 2000000){
    tmp += Math.sqrt(x*x + y*y)
}
end = performance.now();
console.log(tmp, "Math.sqrt operation: " + (end - ini) + " ms");

// Math.hypot

i = 0;
ini = performance.now();
tmp = 0;
while(i++ < 2000000){
    tmp += Math.hypot(x, y)
}
end = performance.now();

console.log(tmp, "Math.hypot: " + (end - ini) + " ms");

Note: In this test, it's used ES6's Math.hypot.

enter image description here

like image 37
Walter Chapilliquen - wZVanG Avatar answered Sep 22 '22 13:09

Walter Chapilliquen - wZVanG