10 comments on “Tippet: No need to square, sum, and sqrt manually anymore”

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

• 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.

2. 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?

3. 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.

4. 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.

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

6. 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.