{"record":{"id":"d1158c526a4ba0fc","repo":"TheAlgorithms/Python","slug":"negative-arguments-are-not-supported","errorCode":null,"errorMessage":"Negative arguments are not supported","messagePattern":"Negative arguments are not supported","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"dynamic_programming/fast_fibonacci.py","lineNumber":20,"sourceCode":"\n\"\"\"\nThis program calculates the nth Fibonacci number in O(log(n)).\nIt's possible to calculate F(1_000_000) in less than a second.\n\"\"\"\n\nfrom __future__ import annotations\n\nimport sys\n\n\ndef fibonacci(n: int) -> int:\n    \"\"\"\n    return F(n)\n    >>> [fibonacci(i) for i in range(13)]\n    [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]\n    \"\"\"\n    if n < 0:\n        raise ValueError(\"Negative arguments are not supported\")\n    return _fib(n)[0]\n\n\n# returns (F(n), F(n-1))\ndef _fib(n: int) -> tuple[int, int]:\n    if n == 0:  # (F(0), F(1))\n        return (0, 1)\n\n    # F(2n) = F(n)[2F(n+1) - F(n)]\n    # F(2n+1) = F(n+1)^2+F(n)^2\n    a, b = _fib(n // 2)\n    c = a * (b * 2 - a)\n    d = a * a + b * b\n    return (d, c + d) if n % 2 else (c, d)\n\n\nif __name__ == \"__main__\":\n    n = int(sys.argv[1])","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/dynamic_programming/fast_fibonacci.py#L2-L38","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\nvalue = fibonacci(count - 2)  # count == 1 -> -1 -> ValueError\n\n# after\nvalue = fibonacci(count - 2) if count >= 2 else 0","handlingStrategy":"validation","validationCode":"if not isinstance(n, int) or n < 0:\n    raise ValueError('n must be a non-negative integer')\nvalue = fibonacci(n)","typeGuard":"def is_non_negative_int(value: object) -> bool:\n    return isinstance(value, int) and value >= 0","tryCatchPattern":"try:\n    value = fibonacci(n)\nexcept ValueError:\n    raise ValueError(f'fibonacci index {n!r} invalid; must be >= 0') from None","preventionTips":["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."],"tags":["python","input-validation","dynamic-programming","math"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}