hypot - Square root of sum of squares

Syntax

c = hypot(a,b)

Description

c = hypot(a,b) returns the element-wise result of the following equation, computed to avoid underflow and overflow:

c = sqrt(abs(a).^2 + abs(b).^2)

Inputs a and b must follow these rules:

hypot returns the following in output c, depending upon the types of inputs:

Examples

Example 1

To illustrate the difference between using the hypot function and coding the basic hypot equation in M-code, create an anonymous function that performs the same function as hypot, but without the consideration to underflow and overflow that hypot offers:

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

Find the upper limit at which your coded function returns a useful value. You can see that this test function reaches its maximum at about 1e154, returning an infinite result at that point:

myhypot(1e153,1e153)
ans =
    1.4142e+153

myhypot(1e154,1e154)
ans =
   Inf

Do the same using the hypot function, and observe that hypot operates on values up to about 1e308, which is approximately equal to the value for realmax on your computer (the largest double-precision floating-point number you can represent on a particular computer):

hypot(1e308,1e308)
ans =
  1.4142e+308

hypot(1e309,1e309)
ans =
   Inf

Example 2

hypot(a,a) theoretically returns sqrt(2)*abs(a), as shown in this example:

x = 1.271161e308;

y = x * sqrt(2)
y =
  1.7977e+308

y = hypot(x,x)
y =
  1.7977e+308

Algorithm

hypot uses FDLIBM, which was developed at SunSoft, a Sun Microsystems business, by Kwok C. Ng, and others. For information about FDLIBM, see http://www.netlib.org.

See Also

sqrt, abs, norm

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS