Alpha max plus beta min algorithm

From testwiki
Jump to navigation Jump to search

Template:Short description Template:Distinguish

The locus of points that give the same value in the algorithm, for different values of alpha and beta

The alpha max plus beta min algorithm[1] 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 |z|=a2+b2 of a complex number Template:Math 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.

The approximation is expressed as |z|=α𝐌𝐚𝐱+β𝐌𝐒𝐧, 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 α0=2cosπ81+cosπ8=0.960433870103... and β0=2sinπ81+cosπ8=0.397824734759..., giving a maximum error of 3.96%.

α β Largest error (%) Mean error (%)
1/1 1/2 11.80 8.68
1/1 1/4 11.61 3.20
1/1 3/8 6.80 4.25
7/8 7/16 12.50 4.91
15/16 15/32 6.25 3.08
α0 β0 3.96 2.41

Improvements

When α<1, |z| becomes smaller than 𝐌𝐚𝐱 (which is geometrically impossible) 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.

|z|=max(𝐌𝐚𝐱,α𝐌𝐚𝐱+β𝐌𝐒𝐧).

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.

Increasing precision: When splitting the line in two like this one could improve precision even more by replacing the first segment by a better estimate than 𝐌𝐚𝐱, and adjust α and β accordingly.

|z|=max(|z0|,|z1|),
|z0|=α0𝐌𝐚𝐱+β0𝐌𝐒𝐧,
|z1|=α1𝐌𝐚𝐱+β1𝐌𝐒𝐧.
α0 β0 α1 β1 Largest error (%)
1 0 7/8 17/32 βˆ’2.65%
1 0 29/32 61/128 +2.4%
1 0 0.898204193266868 0.485968200201465 Β±2.12%
1 1/8 7/8 33/64 βˆ’1.7%
1 5/32 27/32 71/128 1.22%
127/128 3/16 27/32 71/128 βˆ’1.13%

Beware however, that a non-zero β0 would require at least one extra addition and some bit-shifts (or a multiplication), probably nearly doubling the cost and, depending on the hardware, possibly defeat the purpose of using an approximation in the first place.

See also

  • Hypot, a precise function or algorithm that is also safe against overflow and underflow.

References

Template:Reflist