TheAlgorithms/C-Sharp · error · ArgumentException

The order must be greater than or equal to 1.

Error message

The order must be greater than or equal to 1.

What it means

Minkowski.Distance generalizes Manhattan (order=1) and Euclidean (order=2) distances; the order parameter must be a positive integer. When order < 1 the p-norm is undefined (division by 1/p and pow with p<=0), so it throws ArgumentException before comparing dimensions.

Solutions

  1. Pass an order >= 1 (typically 1, 2, or a large value approximating Chebyshev)
  2. Default unset configuration to 2 (Euclidean) instead of 0
  3. Validate the order parameter at the configuration layer before invoking the metric
  4. Catch ArgumentException and fall back to a standard metric

Example fix

// before
Minkowski.Distance(a, b, 0); // throws
// after
int order = configuredOrder >= 1 ? configuredOrder : 2;
Minkowski.Distance(a, b, order);
Defensive patterns

Strategy: validation

Validate before calling

if (order < 1) throw new ArgumentOutOfRangeException(nameof(order), "Minkowski order must be >= 1");

Type guard

bool IsValidMinkowskiOrder(int order) => order >= 1;

Try / catch

try { d = Minkowski.Distance(a, b, order); }
catch (ArgumentException) { d = Euclidean.Distance(a, b); }

Prevention

When it happens

Trigger: Calling Minkowski.Distance(point1, point2, order) with order = 0 or negative — commonly a default-initialized int (0) passed accidentally, or a computed p that underflowed.

Common situations: Config-driven distance metrics where the p parameter is read as 0 when unset; generic code parameterizing p-norms with an uninitialized variable; UI input allowing 0.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/f153fdff5b8c727d. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/LinearAlgebra/Distances/Minkowski.cs:23

/// It is the sum of the lengths of the projections of the line segment between the points onto the
/// coordinate axes, raised to the power of the order and then taking the p-th root.
/// For the case of order = 1, the Minkowski distance degenerates to the Manhattan distance,
/// for order = 2, the usual Euclidean distance is obtained and for order = infinity, the Chebyshev distance is obtained.
/// </summary>
public static class Minkowski
{
    /// <summary>
    /// Calculate Minkowski distance for two N-Dimensional points.
    /// </summary>
    /// <param name="point1">First N-Dimensional point.</param>
    /// <param name="point2">Second N-Dimensional point.</param>
    /// <param name="order">Order of the Minkowski distance.</param>
    /// <returns>Calculated Minkowski distance.</returns>
    public static double Distance(double[] point1, double[] point2, int order)
    {
        if (order < 1)
        {
            throw new ArgumentException("The order must be greater than or equal to 1.");
        }

        if (point1.Length != point2.Length)
        {
            throw new ArgumentException("Both points should have the same dimensionality");
        }

        // distance = (|x1-y1|^p + |x2-y2|^p + ... + |xn-yn|^p)^(1/p)
        return Math.Pow(point1.Zip(point2, (x1, x2) => Math.Pow(Math.Abs(x1 - x2), order)).Sum(), 1.0 / order);
    }
}

View on GitHub (pinned to 96e2905cab)