Hypot

From Wikipedia, the free encyclopedia
Jump to: navigation, search

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[edit]

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

The magnitude of the hypotenuse from (0, 0) to (xy) 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(xy)        θ = atan2(yx)

Implementation[edit]

The difficulty with the naive implementation is that x2 or y2 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:

\begin{align}
r & = \sqrt { x^2 + y^2 } \\
  & = \sqrt { x^2 ( 1 + (y/x)^2) } \\
  & = |x| \sqrt {1 + (y/x)^2 }
\end{align}

The computation of y/x cannot overflow. 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.

Pseudocode:

double hypot(double x,double y)
{
    double t;
    x = abs(x);
    y = abs(y);
    t = min(x,y);
    x = max(x,y);
    t = t/x;
    return x*sqrt(1+t*t);
}

Programming language support[edit]

The function is present in several programming languages:

See also[edit]

References[edit]

  1. ^ https://docs.python.org/3/library/math.html#math.hypot
  2. ^ http://developer.apple.com/DOCUMENTATION/mac/PPCNumerics/PPCNumerics-141.html
  3. ^ http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/ref/hypot.html
  4. ^ http://www.frameworkpascal.com/helphtml/hypot_func.htm
  5. ^ http://www.php.net/hypot
  6. ^ http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html#hypot(double,%20double)
  7. ^ http://www.ruby-doc.org/core/classes/Math.html#M001470
  8. ^ http://golang.org/pkg/math/#Hypot
  9. ^ http://static.rust-lang.org/doc/std/num.html#function-hypot
  10. ^ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/hypot
  11. ^ Single Unix Specification, Open Group, http://www.opengroup.org/onlinepubs/007908799/xsh/hypot.html
  12. ^ 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
  13. ^ The GNU C Library, Mathematics, http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_17.html