In mathematics, Pythagorean addition is a binary operation on the real numbers that computes the length of the hypotenuse of a right triangle, given its two sides. According to the Pythagorean theorem, for a triangle with sides $a$ and $b$ , this length can be calculated as

$a\oplus b={\sqrt {a^{2}+b^{2}}},$ where $\oplus$ denotes the Pythagorean addition operation.

This operation can be used in the conversion of Cartesian coordinates to polar coordinates. It also provides a simple notation and terminology for some formulas when its summands are complicated; for example, the energy-momentum relation in physics becomes

$E=mc^{2}\oplus pc.$ 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.

## Applications

Pythagorean addition (and its implementation as the hypot function) is often used together with the atan2 function to convert from Cartesian coordinates $(x,y)$  to polar coordinates $(r,\theta )$ :

{\begin{aligned}r&=x\oplus y=\operatorname {hypot} (x,y)\\\theta &=\operatorname {atan2} (y,x).\\\end{aligned}}

If measurements $X,Y,Z,\dots$  have independent errors $\Delta _{X},\Delta _{Y},\Delta _{Z},\dots$  respectively, the quadrature method gives the overall error,

$\varDelta _{o}={\sqrt {{\varDelta _{X}}^{2}+{\varDelta _{Y}}^{2}+{\varDelta _{Z}}^{2}+\cdots }}$

whereas the upper limit of the overall error is
$\varDelta _{u}=\varDelta _{X}+\varDelta _{Y}+\varDelta _{Z}+\cdots$

if the errors were not independent.

This is equivalent of finding the magnitude of the resultant of adding orthogonal vectors, each with magnitude equal to the uncertainty, using the Pythagorean theorem.

In signal processing, addition in quadrature is used to find the overall noise from independent sources of noise. For example, if an image sensor gives six digital numbers of shot noise, three of dark current noise and twp of Johnson–Nyquist noise under a specific condition, the overall noise is

$\sigma =6\oplus 3\oplus 2={\sqrt {6^{2}+3^{2}+2^{2}}}=7$

digital numbers, showing the dominance of larger sources of noise.

## Properties

The operation $\oplus$  is associative and commutative, and

${\sqrt {x_{1}^{2}+x_{2}^{2}+\cdots +x_{n}^{2}}}=x_{1}\oplus x_{2}\oplus \cdots \oplus x_{n}.$

This is enough to form the real numbers under $\oplus$  into a commutative semigroup.

The real numbers under $\oplus$  are not a group, because $\oplus$  can never produce a negative number as its result, whereas each element of a group must be the result of multiplication of itself by 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(xy) avoids problems that occur when squaring very large or very small numbers. If calculated using the natural formula,

$r={\sqrt {x^{2}+y^{2}}},$

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.

If either input 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).

### Calculation order

The difficulty with the naive implementation is that $x^{2}+y^{2}$  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|\geq |y|$ , and then to use the equivalent form

{\begin{aligned}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{aligned}}

The computation of $y/x$  cannot overflow unless both $x$  and $y$  are zero. 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:

• When $x$  is much larger than $y$ , $x\oplus y\approx |x|$ , to within machine precision.
• When $x^{2}$  overflows, multiply both $x$  and $x$  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 $y^{2}$  underflows, scale 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.

### Programming language support

The function is present in many programming languages and libraries, including CSS,C++11,D,Go,JavaScript (since ES2015),Julia,Java (since version 1.5),Kotlin,MATLAB,PHP,Python,Ruby,Rust, and Scala.