hypot

Square root of the sum of squares, without overflow.

Category: Mathematics > Elementary Math

Syntax

C = hypot(A, B)

Returns sqrt(abs(A).^2 + abs(B).^2) element by element.

Description

hypot gives the Euclidean length of a two-component vector. Writing sqrt(a^2 + b^2) directly can overflow when a or b is near 1e154 even though the true result is representable; hypot rescales internally and avoids that.

For more than two components use norm, or nest calls as hypot(hypot(a, b), c).

Input arguments

A, B
Real (or complex, using absolute values) arrays of compatible sizes.

Output arguments

C
Nonnegative array: the length of the hypotenuse of a right triangle with legs A and B.

Example

Triangles and huge values

hypot(3, 4)
hypot([3 5], [4 12])
sqrt((1e200)^2 + (1e200)^2)
hypot(1e200, 1e200)
Output
ans = 5
ans = 1×2

     5    13
ans = Inf
ans = 1.4142e+200

See also

sqrt abs norm cart2pol