{"record":{"id":"e291a59897239c6c","repo":"TheAlgorithms/Python","slug":"double-factorial-iterative-not-defined-for-negat","errorCode":null,"errorMessage":"double_factorial_iterative() not defined for negative values","messagePattern":"double_factorial_iterative\\(\\) not defined for negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/double_factorial.py","lineNumber":50,"sourceCode":"    To learn about the theory behind this algorithm:\n    https://en.wikipedia.org/wiki/Double_factorial\n\n    >>> from math import prod\n    >>> all(double_factorial_iterative(i) == prod(range(i, 0, -2)) for i in range(20))\n    True\n    >>> double_factorial_iterative(0.1)\n    Traceback (most recent call last):\n        ...\n    ValueError: double_factorial_iterative() only accepts integral values\n    >>> double_factorial_iterative(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: double_factorial_iterative() not defined for negative values\n    \"\"\"\n    if not isinstance(num, int):\n        raise ValueError(\"double_factorial_iterative() only accepts integral values\")\n    if num < 0:\n        raise ValueError(\"double_factorial_iterative() not defined for negative values\")\n    value = 1\n    for i in range(num, 0, -2):\n        value *= i\n    return value\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":32,"sourceCodeEnd":61,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/double_factorial.py#L32-L61","documentation":"Raised by double_factorial_iterative() in maths/double_factorial.py when num is a negative integer. range(num, 0, -2) with negative num is empty, which would silently return 1 — a wrong answer for an undefined input — so the function explicitly rejects negatives with ValueError instead.","triggerScenarios":"Calling double_factorial_iterative(-1) or any negative int. The guard is num < 0, evaluated after the isinstance check.","commonSituations":"Negative loop bounds from off-by-one arithmetic; user-supplied numbers with a minus sign; subtracting sizes (len(a) - len(b)) that can go negative.","solutions":["Check num >= 0 before calling and raise a domain-specific error in your own code.","Repair the arithmetic that produced the negative input.","Do not 'fix' it by defaulting to 1 — that is exactly the silent wrong answer this guard exists to prevent."],"exampleFix":"# before\ndouble_factorial_iterative(n - 3)  # ValueError when n < 3\n\n# after\nm = n - 3\nif m < 0:\n    raise ValueError(f'invalid argument {m}: must be >= 0')\ndouble_factorial_iterative(m)","handlingStrategy":"validation","validationCode":"if num < 0:\n    raise ValueError(f'double factorial undefined for negative {num}')","typeGuard":null,"tryCatchPattern":"try:\n    r = double_factorial_iterative(num)\nexcept ValueError as e:\n    if 'negative values' in str(e):\n        raise ValueError(f'upstream produced negative num={num}') from e\n    raise","preventionTips":["Never default negative inputs to 1 — the guard exists to stop that silent wrong answer.","Bound-check loop-derived values before factorial-family calls."],"tags":["maths","factorial","iterative","validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}