Cs · Computer Science

How to Apply Alpha-Max-Beta-Min to Three Numbers

Published By the 9to5Science editorial team

At a glance

  1. 1. Take magnitudes

    Use absolute values, since a vector length depends on component magnitudes, not their signs.

  2. 2. Sort the values

    Order the three magnitudes as largest, middle, and smallest.

  3. 3. Choose an extension

    For three inputs, include the middle value; max and min alone discard useful information.

  4. 4. Estimate and compare

    Apply the chosen weights, and use the exact square root when accuracy matters.

The alpha-max-beta-min algorithm is commonly used to estimate the length of a two-component vector without evaluating a square root. For three numbers, there is no single standard two-coefficient formula: a direct extension using only the maximum and minimum ignores the middle value.

If you want to approximate the Euclidean length of a three-dimensional vector (x,y,z)(x,y,z), first take the absolute values and sort them. A simple three-term approximation is

x2+y2+z2 ≈ M+0.5Q+0.25m,\sqrt{x^2+y^2+z^2}\ \approx\ M+0.5Q+0.25m,

where M≥Q≥m≥0M\ge Q\ge m\ge 0 are the largest, middle, and smallest of ∣x∣|x|, ∣y∣|y|, and ∣z∣|z|. These weights are a convenient heuristic, not a uniquely defined or universally optimal set of coefficients.

Why two terms are not enough

For two nonnegative values aa and bb, the alpha-max-beta-min form is

a2+b2 ≈ αmax⁡(a,b)+βmin⁡(a,b).\sqrt{a^2+b^2}\ \approx\ \alpha\max(a,b)+\beta\min(a,b).

The constants α\alpha and β\beta depend on the desired error and how the approximation is tuned. This formula uses both inputs. With three values, applying it to only the largest and smallest discards the middle one.

For instance, the triples (1,1,0)(1,1,0) and (1,1,1)(1,1,1) have the same largest and smallest values, but their exact lengths are 2\sqrt{2} and 3\sqrt{3}. Any expression depending only on the largest and smallest must give the same estimate for both, so it cannot represent both lengths exactly. A three-input approximation should normally include the middle value, using a form such as αM+βQ+γm\alpha M+\beta Q+\gamma m.

A simple three-number algorithm

For a quick approximation, use

A=M+0.5Q+0.25m.A=M+0.5Q+0.25m.

One implementation is:

function approximateLength3(x, y, z):
    a = abs(x)
    b = abs(y)
    c = abs(z)

    M = max(a, b, c)
    m = min(a, b, c)
    Q = a + b + c - M - m

    return M + 0.5 * Q + 0.25 * m

The subtraction finds the middle magnitude after the maximum and minimum have been identified. Ties cause no problem: if two values are equal, the sorted values and their sum are still correct. The algorithm uses comparisons and arithmetic rather than a square root.

Worked example

Estimate the length of the vector (−3,4,−2)(-3,4,-2).

First take absolute values: 33, 44, and 22. Thus M=4M=4, Q=3Q=3, and m=2m=2. The approximation is

A=4+0.5(3)+0.25(2)=6.A=4+0.5(3)+0.25(2)=6.

The exact length is

(−3)2+42+(−2)2=29≈5.385.\sqrt{(-3)^2+4^2+(-2)^2}=\sqrt{29}\approx 5.385.

So this estimate is about 0.6150.615 too large, or approximately 11.4%11.4\% relative to the exact length. That illustrates why this formula is useful only when a rough, inexpensive estimate is acceptable. Its error depends on the relative sizes of the three components.

Choosing coefficients

A more general three-term approximation is

A=αM+βQ+γm.A=\alpha M+\beta Q+\gamma m.

The appropriate coefficients depend on the error criterion. For example, minimizing the worst-case relative error over all nonzero triples is a different design goal from minimizing average error over a particular workload. If the formula is used in a real-time or embedded system, test candidate coefficients against representative inputs and establish an acceptable error bound for that application. Do not assume coefficients tuned for two inputs remain optimal in three dimensions.

If you need a particular accuracy, calculate the Euclidean length directly with a numerically stable hypot function when your language provides one. For example, a three-argument hypot(x, y, z) avoids some overflow and underflow problems that can arise from directly squaring very large or very small inputs.

Common mistakes

  • Using signed inputs in the ordering. Sort ∣x∣|x|, ∣y∣|y|, and ∣z∣|z|, not the original signed values.
  • Calling the two-term expression a three-input formula. Using only the largest and smallest loses information about the middle component.
  • Treating a heuristic as exact. The weighted sum is an approximation; it does not equal the square-root formula for every triple.
  • Using it without checking the error requirement. A fast estimate may be unsuitable for scientific calculations, safety-critical code, or any task requiring a guaranteed tolerance.
  • Mixing up the middle and smallest values. The coefficients belong to the ordered magnitudes M≥Q≥mM\ge Q\ge m; applying them before sorting changes the result depending on input order.

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.