Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Loren on the Art of MATLAB

February 7th, 2008

Why Does MATLAB Have the Function hypot?

Sometimes I am asked questions like "why did you introduce function xyz into MATLAB? And sometimes I have a compelling answer, even for something that looks simple on the surface. Consider the function hypot.

Contents

What Does hypot Compute?

hypot essentially computes the square root of the sum of the squares of 2 inputs. So what's the big deal, right? For "reasonable" input values, there is no big issue.

fhypot = @(a,b) sqrt(abs(a).^2+abs(b).^2);

Other Ways to Compute hypot

Let's create a set of values and compute hypot 3 ways for these: with hypot, fhypot, and sqrt(2)*x. These should all give the same answers, provide a is real and positive.

a = logspace(0,5,6)
format long
aHypot = hypot(a,a)
aFhypot = fhypot(a,a)
aSqrt2 = sqrt(2)*a
a =
           1          10         100        1000       10000      100000
aHypot =
  1.0e+005 *
  Columns 1 through 3
   0.000014142135624   0.000141421356237   0.001414213562373
  Columns 4 through 6
   0.014142135623731   0.141421356237310   1.414213562373095
aFhypot =
  1.0e+005 *
  Columns 1 through 3
   0.000014142135624   0.000141421356237   0.001414213562373
  Columns 4 through 6
   0.014142135623731   0.141421356237310   1.414213562373095
aSqrt2 =
  1.0e+005 *
  Columns 1 through 3
   0.000014142135624   0.000141421356237   0.001414213562373
  Columns 4 through 6
   0.014142135623731   0.141421356237310   1.414213562373095

Results

The results are the same, to within round-off, for the 3 methods here. But what happens if the magnitude of a is larger, near realmax perhaps?

realmax
ans =
    1.797693134862316e+308

For my computer, a 32-bit Windows machine, realmax for doubles is on the order of 10^308. Let's see what happens if a value nearly that large is used with the different versions of hypot.

a = 1e308
aHypot = hypot(a,a)
aFhypot = fhypot(a,a)
aSqrt2 = sqrt(2)*a
a =
    1.000000000000000e+308
aHypot =
    1.414213562373095e+308
aFhypot =
   Inf
aSqrt2 =
    1.414213562373095e+308

What you can see is that the straight-forward method returns Inf instead of a finite answer. And that's why hypot was added to MATLAB, to compute the hypotenuse robustly, avoiding both underflow and overflow.


Get the MATLAB code

Published with MATLAB® 7.5

5 Responses to “Why Does MATLAB Have the Function hypot?”

  1. Quan replied on :

    That was a pretty interesting post Loren. I’m not sure if it will affect the way I do things around here, but it’s something that could come in useful in the future. Thanks for the tip!

  2. Duane Hanselman replied on :

    Before hypot existed, I used a=abs(complex(a,b)); In doing so, I was able to tap the robustness of the abs() function.

  3. Loren replied on :

    Duane-

    Thanks for pointing out that abs is also implemented robustly.

    –Loren

  4. Hal K replied on :

    Loren,
    Does hypot(a,a) any different from norm([a a])?

    Hal

  5. Loren replied on :

    Hal-

    norm is also careful to scale results, like abs and hypot.

    –Loren

Leave a Reply


Loren Shure works on design of the MATLAB language at The MathWorks. She writes here about once a week on MATLAB programming and related topics.

  • J.B. Brown: Ah, and I am at fault for simply testing collinearity with the origin in the example above.
  • J.B. Brown: Indeed, > collinear( [0 3],[0 8],[0 -1e21+2e-15] ) ans = 1 > collinear( [0 3],[0 8],[0 -1e22+2e-15]...
  • OkinawaDolphin: Loren, thank you for telling me where to download timeit. Here are the two functions I just tested...
  • Loren: JB- It looks to me like Ilya’s solution and therefore yours are equivalent to the determinant. As Tim...
  • Loren: OkinawaDolphin, timeit can be downloaded from the File Exchange. Steve Eddins is the author. It does not ship...
  • OkinawaDolphin: It seems that neither R2007a nor R2007b have the function timeit, but I investigated computation time...
  • J.B. Brown: It would appear to me that Ilya Rozenfeld’s solution would be the cleanest. Just to help those who...
  • Loren: Markus- Congratulations on winning! And a nice illustration of how the size matters. Small enough, and the...
  • Markus: Hi Loren, which version is fastest also depends very much on the matrix dimensions. Look at my test function:...
  • Duncan: OkinawaDolphin, Regarding why your third example is slower than your second example, the result is in fact...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.

Related Topics