hypot
Square root of sum of squares (hypotenuse)
Syntax
Description
Examples
Compute Hypotenuse
Compute the hypotenuse of a right triangle with side lengths of 3
and 4
.
C = hypot(3,4)
C = 5
Overflow and Underflow Behavior
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.
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 representable double-precision floating-point number).
hypot(1e308,1e308)
ans = 1.4142e+308
hypot(1e309,1e309)
ans = Inf
Input Arguments
A,B
— Input arrays
scalars | vectors | matrices | multidimensional arrays
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
returns
NaN
.
Data Types: single
| double
Complex Number Support: Yes
More About
IEEE Compliance
For real inputs, hypot
has a few behaviors
that differ from those recommended in the IEEE®-754 Standard.
MATLAB® | IEEE | |
---|---|---|
|
|
|
|
|
|
hypot(Inf,NaN) |
|
|
hypot(-Inf,NaN) |
|
|
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
The
hypot
function fully supports tall arrays. For more information,
see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
If you use
hypot
with single type and double type operands, the generated code might not produce the same result as MATLAB. See Binary Element-Wise Operations with Single and Double Operands (MATLAB Coder).
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
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).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)