TheAlgorithms/Python · error · ValueError

double_factorial_recursive() not defined for negative values

Error message

double_factorial_recursive() not defined for negative values

What it means

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.

Source

Thrown at maths/double_factorial.py:24

    To learn about the theory behind this algorithm:
    https://en.wikipedia.org/wiki/Double_factorial

    >>> from math import prod
    >>> all(double_factorial_recursive(i) == prod(range(i, 0, -2)) for i in range(20))
    True
    >>> double_factorial_recursive(0.1)
    Traceback (most recent call last):
        ...
    ValueError: double_factorial_recursive() only accepts integral values
    >>> double_factorial_recursive(-1)
    Traceback (most recent call last):
        ...
    ValueError: double_factorial_recursive() not defined for negative values
    """
    if not isinstance(n, int):
        raise ValueError("double_factorial_recursive() only accepts integral values")
    if n < 0:
        raise ValueError("double_factorial_recursive() not defined for negative values")
    return 1 if n <= 1 else n * double_factorial_recursive(n - 2)


def double_factorial_iterative(num: int) -> int:
    """
    Compute double factorial using iterative method.

    To learn about the theory behind this algorithm:
    https://en.wikipedia.org/wiki/Double_factorial

    >>> from math import prod
    >>> all(double_factorial_iterative(i) == prod(range(i, 0, -2)) for i in range(20))
    True
    >>> double_factorial_iterative(0.1)
    Traceback (most recent call last):
        ...
    ValueError: double_factorial_iterative() only accepts integral values
    >>> double_factorial_iterative(-1)

View on GitHub (pinned to f5988cc097)

Solutions

  1. Validate n >= 0 at the call site with your own error message.
  2. Fix the index/subtraction logic that produced the negative value.
  3. For gamma-based extension to odd negative values (not supported here), use scipy.special.gamma ratios instead of this function.

Example fix

# before
double_factorial_recursive(len(a) - len(b))  # negative when b is longer

# after
n = len(a) - len(b)
if n < 0:
    raise ValueError('need len(a) >= len(b)')
double_factorial_recursive(n)
Defensive patterns

Strategy: validation

Validate before calling

if n < 0:
    raise ValueError(f'double factorial undefined for negative {n}')

Try / catch

try:
    r = double_factorial_recursive(n)
except ValueError as e:
    if 'negative values' in str(e):
        raise ValueError(f'index math produced negative n={n}') from e
    raise

Prevention

When it happens

Trigger: Calling double_factorial_recursive(-1) or any negative integer. The guard is n < 0, checked after the isinstance check.

Common situations: Negative results from subtractions or index arithmetic (i - len(seq)); user input with a leading minus; testing boundary values at 0 and below.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/49ed6b6ba5c1bf14. Report an issue: GitHub.