{"record":{"id":"37631bcd68399c11","repo":"TheAlgorithms/Python","slug":"n-is-too-large","errorCode":null,"errorMessage":"n is too large","messagePattern":"n is too large","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/fibonacci.py","lineNumber":232,"sourceCode":"    >>> fib_binet(1)\n    [0, 1]\n    >>> fib_binet(5)\n    [0, 1, 1, 2, 3, 5]\n    >>> fib_binet(10)\n    [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]\n    >>> fib_binet(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: n is negative\n    >>> fib_binet(1475)\n    Traceback (most recent call last):\n        ...\n    ValueError: n is too large\n    \"\"\"\n    if n < 0:\n        raise ValueError(\"n is negative\")\n    if n >= 1475:\n        raise ValueError(\"n is too large\")\n    sqrt_5 = sqrt(5)\n    phi = (1 + sqrt_5) / 2\n    return [round(phi**i / sqrt_5) for i in range(n + 1)]\n\n\ndef matrix_pow_np(m: ndarray, power: int) -> ndarray:\n    \"\"\"\n    Raises a matrix to the power of 'power' using binary exponentiation.\n\n    Args:\n        m: Matrix as a numpy array.\n        power: The power to which the matrix is to be raised.\n\n    Returns:\n        The matrix raised to the power.\n\n    Raises:\n        ValueError: If power is negative.","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/fibonacci.py#L214-L250","documentation":"Raised by fib_binet() in maths/fibonacci.py when n >= 1475. Binet's formula raises phi (about 1.618) to the i-th power in IEEE-754 doubles; for i >= 1475 phi**i overflows the float range (phi**1474 is near 1.8e307, the last representable step), so the function refuses such n with ValueError instead of letting `**` raise OverflowError deep inside the comprehension.","triggerScenarios":"Calling fib_binet(1475) or larger (per its doctest). The `if n >= 1475` guard fires before computing phi**i / sqrt_5.","commonSituations":"Using the closed-form formula for large Fibonacci indices (project-euler-style problems, crypto demos, stress tests), or assuming all fib_* helpers in the module share the same domain — fib_matrix_np and fib_iterative handle large n fine while fib_binet does not.","solutions":["Switch to fib_matrix_np(n) or fib_memoization(n) for n >= 1475 — matrix exponentiation is exact with Python ints.","Cap requested n below 1475 if you must keep Binet's formula.","Use math.fibonacci-style exact algorithms (or fib_iterative for moderate n) when correctness at large indices matters."],"exampleFix":"# before\nfib_binet(2000)  # ValueError: n is too large\n\n# after\nfrom maths.fibonacci import fib_binet, fib_matrix_np\nresult = fib_binet(n) if n < 1475 else fib_matrix_np(n)","handlingStrategy":"fallback","validationCode":"if n >= 1475:\n    raise ValueError(f'fib_binet supports n < 1475, got {n}')\nresult = fib_binet(n)","typeGuard":null,"tryCatchPattern":"try:\n    value = fib_binet(n)\nexcept ValueError as exc:\n    if 'too large' in str(exc):\n        value = fib_matrix_np(n)  # exact for large n\n    else:\n        raise","preventionTips":["Route n >= 1475 to fib_matrix_np or another exact integer algorithm.","Treat 1475 as a float-precision limit, not a tunable.","Add a unit test at the boundary (1474 ok, 1475 raises)."],"tags":["math","fibonacci","binet","float-overflow","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}