{"record":{"id":"9252c8771c34d7cf","repo":"TheAlgorithms/Python","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":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/minkowski_distance.py","lineNumber":34,"sourceCode":"\n    >>> minkowski_distance([1.0, 1.0], [2.0, 2.0], 1)\n    2.0\n    >>> minkowski_distance([1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], 2)\n    8.0\n    >>> import numpy as np\n    >>> bool(np.isclose(5.0, minkowski_distance([5.0], [0.0], 3)))\n    True\n    >>> minkowski_distance([1.0], [2.0], -1)\n    Traceback (most recent call last):\n        ...\n    ValueError: The order must be greater than or equal to 1.\n    >>> minkowski_distance([1.0], [1.0, 2.0], 1)\n    Traceback (most recent call last):\n        ...\n    ValueError: Both points must have the same dimension.\n    \"\"\"\n    if order < 1:\n        raise ValueError(\"The order must be greater than or equal to 1.\")\n\n    if len(point_a) != len(point_b):\n        raise ValueError(\"Both points must have the same dimension.\")\n\n    return sum(abs(a - b) ** order for a, b in zip(point_a, point_b)) ** (1 / order)\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":16,"sourceCodeEnd":46,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/minkowski_distance.py#L16-L46","documentation":"Raised by minkowski_distance() in maths/minkowski_distance.py when order < 1. The Minkowski distance of order p is only a valid metric for p >= 1 (p < 1 violates the triangle inequality), so the function rejects such orders before computing sum(abs(a-b)**order) ** (1/order).","triggerScenarios":"minkowski_distance([1.0], [2.0], -1), minkowski_distance(a, b, 0), or minkowski_distance(a, b, 0.5) (the fractional 'metric' that is not a true distance).","commonSituations":"Tuning p as a hyperparameter and sweeping below 1, defaulting p to 0 by mistake, or reading p from config where a typo produces a negative value.","solutions":["Use p >= 1: p=1 is Manhattan, p=2 is Euclidean.","Constrain hyperparameter search ranges to [1, inf).","If you truly need fractional 'distances', implement them separately rather than bypassing this check."],"exampleFix":"# before\nminkowski_distance([1.0, 2.0], [2.0, 3.0], 0.5)\n\n# after\nminkowski_distance([1.0, 2.0], [2.0, 3.0], 1.5)","handlingStrategy":"validation","validationCode":"if order < 1:\n    raise ValueError('Minkowski order must be >= 1')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Restrict hyperparameter sweeps for p to [1, inf).","Remember p=1 Manhattan, p=2 Euclidean; values below 1 are not metrics."],"tags":["math","valueerror","distance-metrics","parameter-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}