Hypot is a mathematical function defined to calculate the length of the hypotenuse of a right-angle triangle. It was designed to avoid errors arising due to limited-precision calculations performed on computers.

Motivation and usage

Calculating the length of the hypotenuse of a triangle is possible using the square-root function on the sum of two squares, but hypot(x, y) avoids problems that occur when squaring very large or very small numbers.

The magnitude of the hypotenuse from (0, 0) to (x, y) can be calculated using

\( r={\sqrt {x^{2}+y^{2}}}. \)

This operation is also known as Pythagorean addition.

However, the squares of very large or small values of x and y may exceed the range of machine precision when calculated on a computer, leading to an inaccurate result caused by arithmetic underflow and/or arithmetic overflow. The hypot function was designed to calculate the result without causing this problem.

The hypot function is often used together with the atan2 function to convert from Cartesian coordinates to polar coordinates:

r = hypot(x, y),

θ = atan2(y, x).

Implementation

The difficulty with the naive implementation is that *x*^{2} or *y*^{2} may overflow or underflow, unless the intermediate result is computed with extended precision. A common implementation technique is to exchange the values, if necessary, so that |*x*| ≥ |*y*|, and then use the equivalent form^{[1]}

\( {\displaystyle {\begin{aligned}r&={\sqrt {x^{2}+y^{2}}}\\&={\sqrt {x^{2}\left(1+\left({\tfrac {y}{x}}\right)^{2}\right)}}\\&=|x|{\sqrt {1+\left({\tfrac {y}{x}}\right)^{2}}}\left(=|x|+{\frac {y}{|x|}}{\frac {y}{1+{\sqrt {1+\left({\tfrac {y}{x}}\right)^{2}}}}}\right).\end{aligned}}} \)

The computation of y/x cannot overflow unless both x and y are 0. If y/x underflows, the final result is equal to |x|, which is correct within the precision of the calculation. The square root is computed of a value between 1 and 2. Finally, the multiplication by |x| cannot underflow, and overflows only when the result is too large to represent.

Programming language support

The function is present in several programming languages:

C99

CSS[2]

C++11[3]

D (programming language)[4]

Fortran 2008

Julia (programming language)[5]

Swift (programming language)

Python (programming language)[6]

Apple's PowerPC Numerics[7]

MATLAB[8]

Pascal[9]

PHP[10]

Java (programming language) (since version 1.5)[11]

Kotlin[12]

Ruby[13]

Go[14]

Rust[15]

JavaScript[16]

Some C90 and C++ libraries have provided a hypot function.[17][18][19]

Scala[20]

See also

Alpha max plus beta min algorithm, a faster algorithm yielding an approximate result

References

In some situations, last form reduces calcuration errors (in ULPs).

Cimpanu, Catalin. "CSS to get support for trigonometry functions". ZDNet. Retrieved 2019-11-01.

http://www.cplusplus.com/reference/cmath/hypot/

https://dlang.org/phobos/std_math.html#.hypot

https://docs.julialang.org/en/v1/base/math/#Base.Math.hypot

https://docs.python.org/3/library/math.html#math.hypot

https://developer.apple.com/DOCUMENTATION/mac/PPCNumerics/PPCNumerics-141.html

http://nl.mathworks.com/help/matlab/ref/hypot.html

http://www.frameworkpascal.com/helphtml/hypot_func.htm

http://www.php.net/hypot

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html#hypot(double,%20double)

"hypot - Kotlin Programming Language". Kotlin. Retrieved 2018-03-19.

http://ruby-doc.org/core/Math.html#method-c-hypot

http://golang.org/pkg/math/#Hypot

https://doc.rust-lang.org/std/primitive.f64.html#method.hypot

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/hypot

Single Unix Specification, Open Group, http://www.opengroup.org/onlinepubs/007908799/xsh/hypot.html

IBM, ILE C/C++ Run-Time Library Functions, http://publib.boulder.ibm.com/infocenter/iadthelp/v7r0/index.jsp?topic=/com.ibm.etools.iseries.langref.doc/rzan5mst144.htm

The GNU C Library, Mathematics, http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_17.html Archived 2009-03-05 at the Wayback Machine

https://www.scala-lang.org/api/current/scala/math/index.html#hypot(x:Double,y:Double):Double

Undergraduate Texts in Mathematics

Graduate Studies in Mathematics

Hellenica World - Scientific Library

Retrieved from "http://en.wikipedia.org/"

All text is available under the terms of the GNU Free Documentation License