{"record":{"id":"49ed6b6ba5c1bf14","repo":"TheAlgorithms/Python","slug":"double-factorial-recursive-not-defined-for-negat","errorCode":null,"errorMessage":"double_factorial_recursive() not defined for negative values","messagePattern":"double_factorial_recursive\\(\\) not defined for negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/double_factorial.py","lineNumber":24,"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_recursive(i) == prod(range(i, 0, -2)) for i in range(20))\n    True\n    >>> double_factorial_recursive(0.1)\n    Traceback (most recent call last):\n        ...\n    ValueError: double_factorial_recursive() only accepts integral values\n    >>> double_factorial_recursive(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: double_factorial_recursive() not defined for negative values\n    \"\"\"\n    if not isinstance(n, int):\n        raise ValueError(\"double_factorial_recursive() only accepts integral values\")\n    if n < 0:\n        raise ValueError(\"double_factorial_recursive() not defined for negative values\")\n    return 1 if n <= 1 else n * double_factorial_recursive(n - 2)\n\n\ndef double_factorial_iterative(num: int) -> int:\n    \"\"\"\n    Compute double factorial using iterative method.\n\n    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)","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/double_factorial.py#L6-L42","documentation":"Raised by double_factorial_recursive() in maths/double_factorial.py when n is an int but negative. The double factorial n!! is defined for non-negative integers (5!! = 15, 0!! = 1!! = 1); a negative n would recurse downward past the base case forever (n - 2 never reaches 0 or 1), so the guard stops it with ValueError.","triggerScenarios":"Calling double_factorial_recursive(-1) or any negative integer. The guard is n < 0, checked after the isinstance check.","commonSituations":"Negative results from subtractions or index arithmetic (i - len(seq)); user input with a leading minus; testing boundary values at 0 and below.","solutions":["Validate n >= 0 at the call site with your own error message.","Fix the index/subtraction logic that produced the negative value.","For gamma-based extension to odd negative values (not supported here), use scipy.special.gamma ratios instead of this function."],"exampleFix":"# before\ndouble_factorial_recursive(len(a) - len(b))  # negative when b is longer\n\n# after\nn = len(a) - len(b)\nif n < 0:\n    raise ValueError('need len(a) >= len(b)')\ndouble_factorial_recursive(n)","handlingStrategy":"validation","validationCode":"if n < 0:\n    raise ValueError(f'double factorial undefined for negative {n}')","typeGuard":null,"tryCatchPattern":"try:\n    r = double_factorial_recursive(n)\nexcept ValueError as e:\n    if 'negative values' in str(e):\n        raise ValueError(f'index math produced negative n={n}') from e\n    raise","preventionTips":["Audit len(a) - len(b) style expressions that can go negative.","Validate recursion seeds at the boundary in recursive helpers."],"tags":["maths","factorial","recursion","validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}