请输入您要查询的百科知识:

 

词条 Hypot
释义

  1. Motivation and usage

  2. Implementation

      Pseudocode  

  3. Programming language support

  4. See also

  5. References

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(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:

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

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:

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)

    x = abs(x);    y = abs(y);    if (y > x) swap(x, y);    if (y == 0.0) return x; // This avoids division by zero because x >= y.    double t = y / x;       // 0 < t <= 1    return x * sqrt(1 + t * t);

Programming language support

The function is present in several programming languages:

  • C99
  • CSS
  • C++11[1]
  • D (programming language)[2]
  • Fortran 2008
  • Julia (programming language)
  • Swift (programming language)
  • Python (programming language)[3]
  • Apple's PowerPC Numerics[4]
  • MATLAB[5]
  • Pascal[6]
  • PHP[7]
  • Java (since version 1.5)[8]
  • Kotlin[9]
  • Ruby[10]
  • Go[11]
  • Rust[12]
  • JavaScript[13]
  • Some C90 and C++ libraries have provided a hypot function.[14][15][16]

See also

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

References

1. ^http://www.cplusplus.com/reference/cmath/hypot/
2. ^https://dlang.org/phobos/std_math.html#.hypot
3. ^https://docs.python.org/3/library/math.html#math.hypot
4. ^https://developer.apple.com/DOCUMENTATION/mac/PPCNumerics/PPCNumerics-141.html
5. ^http://nl.mathworks.com/help/matlab/ref/hypot.html
6. ^http://www.frameworkpascal.com/helphtml/hypot_func.htm
7. ^http://www.php.net/hypot
8. ^http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html#hypot(double,%20double)
9. ^{{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}}
10. ^http://ruby-doc.org/core/Math.html#method-c-hypot
11. ^http://golang.org/pkg/math/#Hypot
12. ^https://doc.rust-lang.org/std/primitive.f64.html#method.hypot
13. ^https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/hypot
14. ^Single Unix Specification, Open Group, http://www.opengroup.org/onlinepubs/007908799/xsh/hypot.html
15. ^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
16. ^The GNU C Library, Mathematics, http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_17.html

3 : Trigonometry|Numerical analysis|Articles with example pseudocode

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/9/25 12:28:40