I hang out a lot with engineers and game programmers, so that means I tend to see a lot of code dealing with physics problems. One of the most commonly used bits of code in both domains has to be the Euclidean distance equation. However, few programmers realize that that equation is a lot easier than it used to be.

You may not know the formal name “Euclidean distance”, but you almost certainly remember the equation to calculate the distance between two points. If point 1 is (x_{1},y_{1}) and point 2 is (x_{2},y_{2}), the distance from point 1 to point 2 is:[mathjax]

$$D = \sqrt{(x_2-x_1)^2 + (y_2-y_1)^2}$$

You might also remember it by the mnemonic “the square root of the sum of squares”. The same formula is used to calculate the length of the hypotenuse of a right-angle triangle. If the lengths of the two (non-hypotenuse) sides are `a` and `b`, the length of the hypotenuse `h` is:

$$h = \sqrt{a^2 + b^2}$$

That’s the same formula as above (with the first point set to (0,0)).

This formula comes up a lot, so you’ll see this *frequently* in game or physics code:

auto h = std::sqrt((a * a) + (b * b));

Or even, in extreme cases:

auto dist = std::sqrt(((x2 - x1) * (x2 - x1)) +, ((y2 - y1) * (y2 - y1)));

Or various permutations thereof.

Thing is, since C++11, we’ve actually had a function in the library that does that calculation: `std::hypot()`

. The two code blocks above can be replaced by:

auto h = std::hypot(a, b);

And:

auto dist = std::hypot(x2 - x1, y2 - y1);

And not only will that be less typing, it will be safer because, unnecessary overflows and underflows in the intermediate steps of the calculation are avoided. Plus, it might be faster. If you’re still writing square-and-sum-then-`sqrt`

code, it’s time to switch!

Sadly, there’s no overload for 3 or 4 variables, which would be handy for working in three dimensions. Still might as well use what we’ve got.

Like pretty much all the math functions in C++11 or later, it’s overloaded for the 3 standard floating-point types (`float`

, `double`

, and `long double`

), and there’s a templated 4^{th} overload for all other built-in arithmetic types that returns `double`

unless one of the arguments is `long double`

(in which case it returns `long double`

).

Tippet: No need to square, sum, and `sqrt`

manually anymore by Explicit C++ is licensed under a Creative Commons Attribution 4.0 International License.

Well, according to my tests, hypot is quite a lot slower than writing it yourself with sqrt. At least with msvc.

Interesting. Any idea why it is slower? Have you looked at the assembly output?

For the record, my own tests show no measurable difference between the two methods on GCC or Clang on Linux. Oddly, hypot seems *slightly* faster with Clang, while the sum+sqrt method seems *slightly* faster with GCC… but all still well within the noise.

Same with GCC on Linux. hypot is a function call, the sqrt is inlined. So if you don’t need the additional overflow/underflow checking that hypot does, I wouldn’t use it. But I am curious why you say, it might be faster. Are there any CPUs where hypot is an assembly instruction?

Thanks for that reminder. C++11 brings a lot of new math functions. I like also cbrt(), which can replace pow(x, 1./3/)

Do you have any idea how to replace:

sqrt(x*x+y*y+z*z) // calc the hypotenuse in 3d

— Sven

cbrt() is nice; fma(), too. I’m also considering replacing “x = (a > 0.0) ? 1.0 : -1.0” with “x = copysign(1.0, a)” or “if (x > 0.0)” with “if (signbit(x))”. They’re not *exactly* the same, but no one really cares about the edge cases most of the time.

Sorry, though. The standard math functions are all 2D. It would be nice to have 3D math – and maybe even quaternions – standardized, but I wouldn’t hold my breath.

sqrt(x*x+y*y+z*z)=hypot(hypot(x,y),z)

And according to my tests, hypot is a lot slower than do it by hand with sqrt too. I use GCC g++ 4.8.2 compiler on Ubuntu, and valgrind (callgrind) to take times. Maybe you could remove “Plus, it might be faster” sentence. Good post anyway.

For 3D one could use hypot(hypot(x,y),z), for 4D hypot(hypot(u,x),hypot(y,z)).

I have just written a program for image edge detection and I had to use that formula, using hypot(x, y) I got quite more than double of time than with sqrt(x*x + y*y). I use Mingw > 4.8.