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

Jump to content

Hypot: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Add Scala
Tag: New redirect
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
#REDIRECT [[Pythagorean addition]]
{{short description|Computationally safe computer function to calculate the hypotenuse of a right triangle}}
{{R from merge}}
'''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

: <math>r = \sqrt{x^2 + y^2}.</math>

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 coordinate system|Cartesian coordinates]] to [[polar coordinate system|polar coordinates]]:

: ''r'' = hypot(''x'',&nbsp;''y''),
: ''θ'' = atan2(''y'',&nbsp;''x'').

==Implementation==
The difficulty with the naive implementation is that ''x''<sup>2</sup> or ''y''<sup>2</sup> 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''|&nbsp;≥&nbsp;|''y''|, and then use the equivalent form<ref>In some situations, last form reduces calcuration errors (in [[Unit in the last place|ULP]]s).</ref>

: <math>\begin{align}
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{align}</math>

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]]<ref>{{Cite web|url=https://www.zdnet.com/article/css-to-get-support-for-trigonometry-functions/|title=CSS to get support for trigonometry functions|last=Cimpanu|first=Catalin|website=ZDNet|language=en|access-date=2019-11-01}}</ref>
*[[C++11]]<ref>http://www.cplusplus.com/reference/cmath/hypot/</ref>
*[[D (programming language)]]<ref>https://dlang.org/phobos/std_math.html#.hypot</ref>
*[[Fortran 2008]]
*[[Julia (programming language)]]<ref>https://docs.julialang.org/en/v1/base/math/#Base.Math.hypot</ref>
*[[Swift (programming language)]]
*[[Python (programming language)]]<ref>https://docs.python.org/3/library/math.html#math.hypot</ref>
*Apple's PowerPC Numerics<ref>https://developer.apple.com/DOCUMENTATION/mac/PPCNumerics/PPCNumerics-141.html</ref>
*MATLAB<ref>http://nl.mathworks.com/help/matlab/ref/hypot.html</ref>
*Pascal<ref>http://www.frameworkpascal.com/helphtml/hypot_func.htm</ref>
*PHP<ref>http://www.php.net/hypot</ref>
*[[Java (programming language)]] (since version 1.5)<ref>http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html#hypot(double,%20double)</ref>
*Kotlin<ref>{{Cite web|url=https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.math/hypot.html|title=hypot - Kotlin Programming Language|website=Kotlin|access-date=2018-03-19}}</ref>
*Ruby<ref>http://ruby-doc.org/core/Math.html#method-c-hypot</ref>
*Go<ref>http://golang.org/pkg/math/#Hypot</ref>
*Rust<ref>https://doc.rust-lang.org/std/primitive.f64.html#method.hypot</ref>
*JavaScript<ref>https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/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 {{Webarchive|url=https://web.archive.org/web/20090305023614/http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_17.html |date=2009-03-05 }}</ref>
*[[Scala]]<ref>https://www.scala-lang.org/api/current/scala/math/index.html#hypot(x:Double,y:Double):Double</ref>

==See also==
*[[Alpha max plus beta min algorithm]], a faster algorithm yielding an approximate result

==References==
<references/>

[[Category:Trigonometry]]
[[Category:Numerical analysis]]
[[Category:Pythagorean theorem]]
[[Category:Articles with example pseudocode]]

Latest revision as of 23:37, 4 October 2021

  • From a merge: This is a redirect from a page that was merged into another page. This redirect was kept in order to preserve the edit history of this page after its content was merged into the content of the target page. Please do not remove the tag that generates this text (unless the need to recreate content on this page has been demonstrated) or delete this page.