TheAlgorithms/Python · error · ValueError
double_factorial_iterative() not defined for negative values
Error message
double_factorial_iterative() not defined for negative values
What it means
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.
Source
Thrown at maths/double_factorial.py:50
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)
Traceback (most recent call last):
...
ValueError: double_factorial_iterative() not defined for negative values
"""
if not isinstance(num, int):
raise ValueError("double_factorial_iterative() only accepts integral values")
if num < 0:
raise ValueError("double_factorial_iterative() not defined for negative values")
value = 1
for i in range(num, 0, -2):
value *= i
return value
if __name__ == "__main__":
import doctest
doctest.testmod()
View on GitHub (pinned to f5988cc097)
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.
Example fix
# before
double_factorial_iterative(n - 3) # ValueError when n < 3
# after
m = n - 3
if m < 0:
raise ValueError(f'invalid argument {m}: must be >= 0')
double_factorial_iterative(m) Defensive patterns
Strategy: validation
Validate before calling
if num < 0:
raise ValueError(f'double factorial undefined for negative {num}') Try / catch
try:
r = double_factorial_iterative(num)
except ValueError as e:
if 'negative values' in str(e):
raise ValueError(f'upstream produced negative num={num}') from e
raise Prevention
- Never default negative inputs to 1 — the guard exists to stop that silent wrong answer.
- Bound-check loop-derived values before factorial-family calls.
When it happens
Trigger: Calling double_factorial_iterative(-1) or any negative int. The guard is num < 0, evaluated after the isinstance check.
Common situations: 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.
Related errors
- double_factorial_iterative() only accepts integral values
- double_factorial_recursive() only accepts integral values
- double_factorial_recursive() not defined for negative values
- factorial() only accepts integral values
- factorial() not defined for negative values
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/e291a59897239c6c.
Report an issue: GitHub.