Alpha max plus beta min algorithm


The alpha max plus beta min algorithm is a high-speed approximation of the square root of the sum of two squares. The square root of the sum of two squares, also known as Pythagorean addition, is a useful function, because it finds the hypotenuse of a right triangle given the two side lengths, the norm of a 2-D vector, or the magnitude of a complex number given the real and imaginary parts.
The algorithm avoids performing the square and square-root operations, instead using simple operations such as comparison, multiplication, and addition. Some choices of the α and β parameters of the algorithm allow the multiplication operation to be reduced to a simple shift of binary digits that is particularly well suited to implementation in high-speed digital circuitry.

Formulation

The approximation is expressed as
where is the maximum absolute value of a and b, and is the minimum absolute value of a and b.
For the closest approximation, the optimum values for and are and, giving a maximum error of 3.96%.
Largest error Mean error
1/11/211.808.68
1/11/411.613.20
1/13/86.804.25
7/87/1612.504.91
15/1615/326.253.08
3.962.41

Improvements

When, becomes smaller than near the axes where is near 0.
This can be remedied by replacing the result with whenever that is greater, essentially splitting the line into two different segments.
Depending on the hardware, this improvement can be almost free.
Using this improvement changes which parameter values are optimal, because they no longer need a close match for the entire interval. A lower and higher can therefore increase precision further.
This can be improved even further by, instead of using as the second estimate, use a second pair of parameters and, with and adjusted accordingly.
Largest error
107/817/32−2.66%
1029/3261/128+2.40%
100.8982041932668680.485968200201465±2.12%
11/87/833/64−1.67%
15/3227/3271/128+1.21%
127/1283/1627/3271/128−1.12%

Of course, a non-zero requires at least one extra addition and some bit-shifts, nearly doubling the cost and, depending on the hardware, possibly defeating the purpose of using an approximation in the first place.