Hypot: Difference between revisions
Copyeditor42 (talk | contribs) JavaScript since ES2015, add link |
Citation bot (talk | contribs) Add: title, eprint. Changed bare reference to CS1/2. Removed parameters. Some additions/deletions were parameter name changes. | Use this bot. Report bugs. | Suggested by Abductive | Category:Trigonometry | #UCB_Category 24/105 |
||
Line 49: | Line 49: | ||
Additional techniques allow the result to be computed more accurately, e.g. to less than one [[Unit in the last place|ulp]].<ref> |
Additional techniques allow the result to be computed more accurately, e.g. to less than one [[Unit in the last place|ulp]].<ref> |
||
{{cite arxiv |
{{cite arxiv |
||
| |
|eprint=1904.09481 |class=math.NA |
||
|title=An Improved Algorithm for hypot(a,b) |
|title=An Improved Algorithm for hypot(a,b) |
||
|first=Carlos F. |last=Borges |
|first=Carlos F. |last=Borges |
||
Line 60: | Line 60: | ||
*[[C99]] |
*[[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> |
*[[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> |
*[[C++11]]<ref>{{Cite web|url=http://www.cplusplus.com/reference/cmath/hypot/|title = Hypot - C++ Reference}}</ref> |
||
*[[D (programming language)]]<ref>https://dlang.org/phobos/std_math.html#.hypot</ref> |
*[[D (programming language)]]<ref>{{Cite web|url=https://dlang.org/phobos/std_math.html#.hypot|title=STD.math - D Programming Language}}</ref> |
||
*[[Fortran 2008]] |
*[[Fortran 2008]] |
||
*[[Julia (programming language)]]<ref>https://docs.julialang.org/en/v1/base/math/#Base.Math.hypot</ref> |
*[[Julia (programming language)]]<ref>{{Cite web|url=https://docs.julialang.org/en/v1/base/math/#Base.Math.hypot|title = Mathematics · the Julia Language}}</ref> |
||
*[[Swift (programming language)]] |
*[[Swift (programming language)]] |
||
*[[Python (programming language)]]<ref>https://docs.python.org/3/library/math.html#math.hypot</ref> |
*[[Python (programming language)]]<ref>{{Cite web|url=https://docs.python.org/3/library/math.html#math.hypot|title=Math — Mathematical functions — Python 3.9.7 documentation}}</ref> |
||
*Apple's PowerPC Numerics<ref>https://developer.apple.com/DOCUMENTATION/mac/PPCNumerics/PPCNumerics-141.html</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> |
*MATLAB<ref>{{Cite web|url=http://nl.mathworks.com/help/matlab/ref/hypot.html|title = Square root of sum of squares (Hypotenuse) - MATLAB hypot - MathWorks Benelux}}</ref> |
||
*Pascal<ref>http://www.frameworkpascal.com/helphtml/hypot_func.htm</ref> |
*Pascal<ref>{{Cite web|url=http://www.frameworkpascal.com/helphtml/hypot_func.htm|title=Hypot function}}</ref> |
||
*PHP<ref>http://www.php.net/hypot</ref> |
*PHP<ref>{{Cite web|url=http://www.php.net/hypot|title=PHP: Hypot - Manual}}</ref> |
||
*[[Java (programming language)]] (since version 1.5)<ref>http://java.sun.com/j2se/1.5.0/docs/enwiki/api/java/lang/Math.html#hypot(double,%20double)</ref> |
*[[Java (programming language)]] (since version 1.5)<ref>{{Cite web|url=http://java.sun.com/j2se/1.5.0/docs/enwiki/api/java/lang/Math.html#hypot(double,%20double)|title = Math (Java 2 Platform SE 5.0)}}</ref> |
||
*Kotlin<ref>{{Cite web|url=https://kotlinlang.org/enwiki/api/latest/jvm/stdlib/kotlin.math/hypot.html|title=hypot - Kotlin Programming Language|website=Kotlin|access-date=2018-03-19}}</ref> |
*Kotlin<ref>{{Cite web|url=https://kotlinlang.org/enwiki/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> |
*Ruby<ref>{{Cite web|url=http://ruby-doc.org/core/Math.html#method-c-hypot|title = Module: Math (Ruby 3.0.2)}}</ref> |
||
*Go<ref>http://golang.org/pkg/math/#Hypot</ref> |
*Go<ref>{{Cite web|url=http://golang.org/pkg/math/#Hypot|title=Math package - math - PKG.go.dev}}</ref> |
||
*Rust<ref>https://doc.rust-lang.org/std/primitive.f64.html#method.hypot</ref> |
*Rust<ref>{{Cite web|url=https://doc.rust-lang.org/std/primitive.f64.html#method.hypot|title = F64 - Rust}}</ref> |
||
*[[JavaScript]] (since ES2015)<ref>https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/hypot</ref> |
*[[JavaScript]] (since ES2015)<ref>{{Cite web|url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/hypot|title = Math.hypot() - JavaScript | MDN}}</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> |
*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 (programming language)|Scala]]<ref>https://www.scala-lang.org/enwiki/api/current/scala/math/index.html#hypot(x:Double,y:Double):Double</ref> |
*[[Scala (programming language)|Scala]]<ref>{{Cite web|url=https://www.scala-lang.org/enwiki/api/current/scala/math/index.html#hypot(x:Double,y:Double):Double|title = Scala Standard Library 2.13.6 - scala.math}}</ref> |
||
==See also== |
==See also== |
Revision as of 10:26, 29 September 2021
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
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).
If either input is infinite, the result is infinite, i.e.
- hypot(x, ±∞) = hypot(±∞, x) = +∞
Because this is true for all possible values of x, including infinity, the IEEE 754 floating-point standard requires that this definition also applies if x is not a number (NaN).[1]
Implementation
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[2]
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.
This implementation has the downside that it requires an additional floating-point division, which can double the cost of the naive implementation, as multiplication and addition are typically far faster than division and square root.
More complex implementations avoid this by dividing the inputs into more cases:
- x ≫ y: hypot(x, y) = |x|, to within machine precision.
- x2 overflows: Multiply both x and y by a small scaling factor (e.g. 2−64 for IEEE single precision), use the naive algorithm which will now not overflow, and multiply the result by the (large) inverse (e.g. 264).
- y2 underflows: As above, but reverse the scaling factors to scale up the intermediate values.
- Otherwise: The naive algorithm is safe to use.
Additional techniques allow the result to be computed more accurately, e.g. to less than one ulp.[3]
Programming language support
The function is present in several programming languages:
- C99
- CSS[4]
- C++11[5]
- D (programming language)[6]
- Fortran 2008
- Julia (programming language)[7]
- Swift (programming language)
- Python (programming language)[8]
- Apple's PowerPC Numerics[9]
- MATLAB[10]
- Pascal[11]
- PHP[12]
- Java (programming language) (since version 1.5)[13]
- Kotlin[14]
- Ruby[15]
- Go[16]
- Rust[17]
- JavaScript (since ES2015)[18]
- Some C90 and C++ libraries have provided a hypot function.[19][20][21]
- Scala[22]
See also
- Alpha max plus beta min algorithm, a faster algorithm yielding an approximate result
References
- ^ Fog, Agner (2020-04-27). "Floating point exception tracking and NAN propagation" (PDF). p. 6.
- ^ In some situations, last form reduces calculation errors (in ULPs).
- ^ Borges, Carlos F. (14 June 2019). "An Improved Algorithm for hypot(a,b)". arXiv:1904.09481 [math.NA].
- ^ Cimpanu, Catalin. "CSS to get support for trigonometry functions". ZDNet. Retrieved 2019-11-01.
- ^ "Hypot - C++ Reference".
- ^ "STD.math - D Programming Language".
- ^ "Mathematics · the Julia Language".
- ^ "Math — Mathematical functions — Python 3.9.7 documentation".
- ^ https://developer.apple.com/DOCUMENTATION/mac/PPCNumerics/PPCNumerics-141.html
- ^ "Square root of sum of squares (Hypotenuse) - MATLAB hypot - MathWorks Benelux".
- ^ "Hypot function".
- ^ "PHP: Hypot - Manual".
- ^ "Math (Java 2 Platform SE 5.0)".
- ^ "hypot - Kotlin Programming Language". Kotlin. Retrieved 2018-03-19.
- ^ "Module: Math (Ruby 3.0.2)".
- ^ "Math package - math - PKG.go.dev".
- ^ "F64 - Rust".
- ^ "Math.hypot() - JavaScript | MDN".
- ^ 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
- ^ "Scala Standard Library 2.13.6 - scala.math".