Nothing Special   »   [go: up one dir, main page]

Jump to content

Hypot: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Programming language support: There's a canonical resource for the reference, which is better than a site that's hated by the community in question (by the veterans anyway)
Line 53: Line 53:
* Ruby <ref>http://www.ruby-doc.org/core/classes/Math.html#M001470</ref>
* Ruby <ref>http://www.ruby-doc.org/core/classes/Math.html#M001470</ref>
* Go <ref>http://golang.org/pkg/math/#Hypot</ref>
* Go <ref>http://golang.org/pkg/math/#Hypot</ref>
* Rust <ref>http://static.rust-lang.org/doc/std/num.html#function-hypot</ref>


Some C90 and C++ libraries have provided a hypot function.<ref>Single Unix Specification, Open Group, http://www.opengroup.org/onlinepubs/007908799/xsh/hypot.html</ref><ref>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</ref><ref>The GNU C Library, Mathematics, http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_17.html</ref>
Some C90 and C++ libraries have provided a hypot function.<ref>Single Unix Specification, Open Group, http://www.opengroup.org/onlinepubs/007908799/xsh/hypot.html</ref><ref>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</ref><ref>The GNU C Library, Mathematics, http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_17.html</ref>

Revision as of 21:05, 24 August 2013

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

Calculation of the length of the hypotenuse of a triangle is possible to do using the square root function but hypot(xy) avoids possible problems with very large or very small numbers.

The magnitude of the hypotenuse from (0, 0) to (xy) can be calculated using:

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 (see underflow, overflow). The hypot function was designed to calculate the result without causing this problem.

The hypot function may typically be used together with the atan2 function to convert from Cartesian to polar coordinates:

 r = hypot(xy)        θ = atan2(yx)

This operation is also known as Pythagorean addition.

Implementation

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

The computation of y/x cannot overflow, and underflows compute the correct result. The square root is computed over 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

The function is present in several programming languages:

Some C90 and C++ libraries have provided a hypot function.[9][10][11]

See also

References