TheAlgorithms/Python · error · ValueError
Negative arguments are not supported
Error message
Negative arguments are not supported
What it means
Raised by fibonacci(n) (fast doubling implementation) when n is negative. The fast-doubling identities compute F(2n) and F(2n+1) from F(n), which is undefined for negative indices in this implementation, so the guard rejects them up front with a ValueError. Valid calls return F(n) in O(log n) time.
Source
Thrown at dynamic_programming/fast_fibonacci.py:20
"""
This program calculates the nth Fibonacci number in O(log(n)).
It's possible to calculate F(1_000_000) in less than a second.
"""
from __future__ import annotations
import sys
def fibonacci(n: int) -> int:
"""
return F(n)
>>> [fibonacci(i) for i in range(13)]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
"""
if n < 0:
raise ValueError("Negative arguments are not supported")
return _fib(n)[0]
# returns (F(n), F(n-1))
def _fib(n: int) -> tuple[int, int]:
if n == 0: # (F(0), F(1))
return (0, 1)
# F(2n) = F(n)[2F(n+1) - F(n)]
# F(2n+1) = F(n+1)^2+F(n)^2
a, b = _fib(n // 2)
c = a * (b * 2 - a)
d = a * a + b * b
return (d, c + d) if n % 2 else (c, d)
if __name__ == "__main__":
n = int(sys.argv[1])View on GitHub (pinned to f5988cc097)
Solutions
- Guard the call site: use fibonacci(n) only when n >= 0, e.g. fibonacci(max(0, n)) if clamping is acceptable.
- Fix the upstream index computation that produced the negative value.
- If negative indices (negafibonacci) are genuinely needed, implement F(-n) = (-1)^(n+1) * F(n) yourself around this function.
Example fix
# before value = fibonacci(count - 2) # count == 1 -> -1 -> ValueError # after value = fibonacci(count - 2) if count >= 2 else 0
Defensive patterns
Strategy: validation
Validate before calling
if not isinstance(n, int) or n < 0:
raise ValueError('n must be a non-negative integer')
value = fibonacci(n) Type guard
def is_non_negative_int(value: object) -> bool:
return isinstance(value, int) and value >= 0 Try / catch
try:
value = fibonacci(n)
except ValueError:
raise ValueError(f'fibonacci index {n!r} invalid; must be >= 0') from None Prevention
- Validate index arithmetic (len(x) - k) before calling sequence functions.
- Add boundary tests with n = 0 and negative values for all sequence APIs.
- If negafibonacci is needed, wrap with F(-n) = (-1) ** (n + 1) * F(n) rather than passing negatives.
When it happens
Trigger: fibonacci(-1) or any negative n, typically from n = len(seq) - 2 style computations on short sequences, or from signed offsets/index arithmetic that underflows past zero.
Common situations: Index math on lists shorter than expected (e.g. fibonacci(len(items) - 3) with 2 items); CLI/config values parsed as negative; passing a negative Fibonacci index expecting negafibonacci support, which this function does not implement.
Related errors
- Limit for the Catalan sequence must be ≥ 0
- the value of input must be a natural number
- Number should not be negative.
- iterations must be defined as integers
- starting number must be and integer
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/d1158c526a4ba0fc.
Report an issue: GitHub.