{"record":{"id":"f153fdff5b8c727d","repo":"TheAlgorithms/C-Sharp","slug":"the-order-must-be-greater-than-or-equal-to-1","errorCode":null,"errorMessage":"The order must be greater than or equal to 1.","messagePattern":"The order must be greater than or equal to 1\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/LinearAlgebra/Distances/Minkowski.cs","lineNumber":23,"sourceCode":"/// It is the sum of the lengths of the projections of the line segment between the points onto the\n/// coordinate axes, raised to the power of the order and then taking the p-th root.\n/// For the case of order = 1, the Minkowski distance degenerates to the Manhattan distance,\n/// for order = 2, the usual Euclidean distance is obtained and for order = infinity, the Chebyshev distance is obtained.\n/// </summary>\npublic static class Minkowski\n{\n    /// <summary>\n    /// Calculate Minkowski distance for two N-Dimensional points.\n    /// </summary>\n    /// <param name=\"point1\">First N-Dimensional point.</param>\n    /// <param name=\"point2\">Second N-Dimensional point.</param>\n    /// <param name=\"order\">Order of the Minkowski distance.</param>\n    /// <returns>Calculated Minkowski distance.</returns>\n    public static double Distance(double[] point1, double[] point2, int order)\n    {\n        if (order < 1)\n        {\n            throw new ArgumentException(\"The order must be greater than or equal to 1.\");\n        }\n\n        if (point1.Length != point2.Length)\n        {\n            throw new ArgumentException(\"Both points should have the same dimensionality\");\n        }\n\n        // distance = (|x1-y1|^p + |x2-y2|^p + ... + |xn-yn|^p)^(1/p)\n        return Math.Pow(point1.Zip(point2, (x1, x2) => Math.Pow(Math.Abs(x1 - x2), order)).Sum(), 1.0 / order);\n    }\n}\n","sourceCodeStart":5,"sourceCodeEnd":35,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/LinearAlgebra/Distances/Minkowski.cs#L5-L35","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass an order >= 1 (typically 1, 2, or a large value approximating Chebyshev)","Default unset configuration to 2 (Euclidean) instead of 0","Validate the order parameter at the configuration layer before invoking the metric","Catch ArgumentException and fall back to a standard metric"],"exampleFix":"// before\nMinkowski.Distance(a, b, 0); // throws\n// after\nint order = configuredOrder >= 1 ? configuredOrder : 2;\nMinkowski.Distance(a, b, order);","handlingStrategy":"validation","validationCode":"if (order < 1) throw new ArgumentOutOfRangeException(nameof(order), \"Minkowski order must be >= 1\");","typeGuard":"bool IsValidMinkowskiOrder(int order) => order >= 1;","tryCatchPattern":"try { d = Minkowski.Distance(a, b, order); }\ncatch (ArgumentException) { d = Euclidean.Distance(a, b); }","preventionTips":["Default order config to 2 rather than 0","Validate metric parameters when reading configuration","Restrict UI/config inputs for p to positive integers"],"tags":["csharp","linear-algebra","distance","invalid-parameter"],"backgroundTag":"invalid-argument-value","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}