hypot - Square root of sum of squares (hypotenuse) - MATLAB (original) (raw)

Main Content

Square root of sum of squares (hypotenuse)

Syntax

Description

C = hypot([A,B](#bu4zy7c-AB)) returns the hypotenuse of a right triangle with side lengths of A and B. This function computes C = sqrt(abs(A).^2 + abs(B).^2) by avoiding underflow and overflow.

example

Examples

collapse all

Compute the hypotenuse of a right triangle with side lengths of 3 and 4.

Examine the difference between using hypot and coding the basic hypot equation in M-code.

Create an anonymous function that performs essentially the same basic function as hypot.

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

myhypot does not have the same consideration for underflow and overflow behavior that hypot offers.

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

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 representable double-precision floating-point number).

Input Arguments

collapse all

Input arrays, specified as scalars, vectors, matrices, or multidimensional arrays. Inputs A and B must either be the same size or have sizes that are compatible (for example, A is an M-by-N matrix and B is a scalar or 1-by-N row vector). For more information, see Compatible Array Sizes for Basic Operations.

If one or both inputs is NaN, then hypot returnsNaN.

Data Types: single | double
Complex Number Support: Yes

More About

collapse all

For real inputs, hypot has a few behaviors that differ from those recommended in the IEEEĀ®-754 Standard.

| | MATLABĀ® | IEEE | | | --------------- | ---- | --- | | hypot(NaN,Inf) | NaN | Inf | | hypot(NaN,-Inf) | NaN | Inf | | hypot(Inf,NaN) | NaN | Inf | | hypot(-Inf,NaN) | NaN | Inf |

Extended Capabilities

expand all

Thehypot function fully supports tall arrays. For more information, see Tall Arrays.

The hypot function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced before R2006a