It is implemented in many programming libraries as the hypot function, in a way designed to avoid errors arising due to limited-precision calculations performed on computers. In its applications to
signal processing and
propagation of
measurement uncertainty, the same operation is also called addition in quadrature;[2] it is related to the quadratic mean or "root mean square".
Applications
Example of Pythagorean addition of independent errors using vector addition of orthogonal vectors
The real numbers under are not a
group, because can never produce a negative number as its result, whereas each element of a group must be the result of applying the group operation to itself and the
identity element. On the non-negative numbers, it is still not a group, because Pythagorean addition of one number by a second positive number can only increase the first number, so no positive number can have an
inverse element. Instead, it forms a
commutative monoid on the non-negative numbers, with zero as its identity.
Implementation
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. Calculating the length of the hypotenuse of a triangle is possible using the
square root function on the sum of two
squares, but hypot avoids problems that occur when squaring very large or very small numbers. If calculated using the natural formula,
the squares of very large or small values of and may exceed the range of
machine precision when calculated on a computer, leading to an inaccurate result caused by
arithmetic underflow and
overflow. The hypot function was designed to calculate the result without causing this problem.[8]
If either input to hypot is infinite, the result is infinite. Because this is true for all possible values of the other input, the
IEEE 754 floating-point standard requires that this remains true even when the other input is
not a number (NaN).[9]
Since
C++17, there has been an additional hypot function for 3D calculations:[10]
Calculation order
The difficulty with the naive implementation is that 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 , and then to use the equivalent form
The computation of cannot overflow unless both and are zero. If underflows, the final result is equal to , 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 cannot underflow, and overflows only when the result is too large to represent.[8] 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. Typically, the implementation is slower by a factor of 2.5 to 3.[11]
More complex implementations avoid this by dividing the inputs into more cases:
When overflows, multiply both and 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).
When underflows, scale as above but reverse the scaling factors to scale up the intermediate values.
Otherwise, the naive algorithm is safe to use.
However, this implementation is extremely slow when it causes incorrect jump predictions due to different cases. Additional techniques allow the result to be computed more accurately, e.g. to less than one
ulp.[8]
Metafont has Pythagorean addition and subtraction as built-in operations, under the names ++ and +-+ respectively.
References
^Moler, Cleve; Morrison, Donald (1983). "Replacing square roots by Pythagorean sums". IBM Journal of Research and Development. 27 (6): 577–581.
CiteSeerX10.1.1.90.5651.
doi:
10.1147/rd.276.0577.
^Johnson, David L. (2017).
"12.2.3 Addition in Quadrature". Statistical Tools for the Comprehensive Practice of Industrial Hygiene and Environmental Health Sciences. John Wiley & Sons. p. 289.
ISBN9781119143017.
^"SIN (3M): Trigonometric functions and their inverses". Unix Programmer's Manual: Reference Guide (4.3 Berkeley Software Distribution Virtual VAX-11 Version ed.). Department of Electrical Engineering and Computer Science, University of California, Berkeley. April 1986.
^
abcBorges, Carlos F. (2021). "Algorithm 1014: An Improved Algorithm for hypot(x, y)". ACM Transactions on Mathematical Software. 47 (1): 9:1–9:12.
arXiv:1904.09481.
doi:
10.1145/3428446.
S2CID230588285.