{"record":{"id":"e2992b8b2f97cbad","repo":"TheAlgorithms/Java","slug":"input-n-is-too-big-to-give-accurate-result","errorCode":null,"errorMessage":"Input 'n' is too big to give accurate result.","messagePattern":"Input 'n' is too big to give accurate result\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/FibonacciNumberGoldenRation.java","lineNumber":46,"sourceCode":"     * Reducing the limit to 70 due to potential floating-point arithmetic errors\n     * that may result in incorrect results for larger inputs.\n     */\n    public static final int MAX_ARG = 70;\n\n    /**\n     * Calculates the nth Fibonacci number using Binet's formula.\n     *\n     * @param n The index of the Fibonacci number to calculate.\n     * @return The nth Fibonacci number as a long.\n     * @throws IllegalArgumentException if the input 'n' is negative or exceeds the range of a long data type.\n     */\n    public static long compute(int n) {\n        if (n < 0) {\n            throw new IllegalArgumentException(\"Input 'n' must be a non-negative integer.\");\n        }\n\n        if (n > MAX_ARG) {\n            throw new IllegalArgumentException(\"Input 'n' is too big to give accurate result.\");\n        }\n\n        if (n <= 1) {\n            return n;\n        }\n\n        // Calculate the nth Fibonacci number using the golden ratio formula\n        final double sqrt5 = Math.sqrt(5);\n        final double phi = (1 + sqrt5) / 2;\n        final double psi = (1 - sqrt5) / 2;\n        final double result = (Math.pow(phi, n) - Math.pow(psi, n)) / sqrt5;\n\n        // Round to the nearest integer and return as a long\n        return Math.round(result);\n    }\n}\n","sourceCodeStart":28,"sourceCodeEnd":63,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/FibonacciNumberGoldenRation.java#L28-L63","documentation":"Thrown by FibonacciNumberGoldenRation.compute(int n) when n exceeds MAX_ARG (70). This class uses Binet's formula — a closed-form golden-ratio expression — which relies on double-precision floating-point arithmetic. For n > 70, floating-point rounding errors accumulate enough to produce incorrect Fibonacci values, so the library refuses to return a silently-wrong result. The guard is a data-integrity safeguard, not an overflow check.","triggerScenarios":"Calling compute(n) with any int value strictly greater than 70. For example compute(71), compute(100), or compute(Integer.MAX_VALUE). Note that the result would still fit in a long for some of these (long overflows only at n ≈ 92), but precision is already lost before that.","commonSituations":"Developer switches from an iterative or matrix-exponentiation Fibonacci implementation to Binet's formula expecting O(1) performance and passes the same large indices. Or a caller reads the Javadoc '@return The nth Fibonacci number as a long' and assumes the full long range is supported.","solutions":["If you need Fibonacci numbers for n > 70, use an exact method: FibonacciLoop (iterative), com.thealgorithms.dynamicprogramming.Fibonacci, or com.thealgorithms.matrix.matrixexponentiation.Fibonacci (O(log n)).","If you must use this class, cap n at 70 before calling compute().","If you only need values up to 92 (the long overflow boundary) but with exactness, switch to an iterative BigInteger-based approach."],"exampleFix":"// before\nlong fib = FibonacciNumberGoldenRation.compute(85);\n\n// after — use exact iterative method for n > 70\nlong fib = (n <= 70)\n    ? FibonacciNumberGoldenRation.compute(n)\n    : FibonacciLoop.fibonacciNumber(n);","handlingStrategy":"validation","validationCode":"if (n < 0 || n > FibonacciNumberGoldenRation.MAX_ARG) {\n    throw new IllegalArgumentException(\"n must be in [0, \" + FibonacciNumberGoldenRation.MAX_ARG + \"]\");\n}\nlong fib = FibonacciNumberGoldenRation.compute(n);","typeGuard":null,"tryCatchPattern":"try {\n    long fib = FibonacciNumberGoldenRation.compute(n);\n} catch (IllegalArgumentException e) {\n    // n is negative or > 70; use an exact method for large n\n    fib = FibonacciLoop.fibonacciNumber(n);\n}","preventionTips":["Check n against FibonacciNumberGoldenRation.MAX_ARG (70) before calling.","For n > 70, prefer FibonacciLoop or matrix-exponentiation Fibonacci for exact results.","Document the precision limit in your own API if you wrap this method."],"tags":["math","fibonacci","floating-point","argument-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}