TheAlgorithms/Java · error · IllegalArgumentException

Input 'n' is too big to give accurate result.

Error message

Input 'n' is too big to give accurate result.

What it means

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.

Source

Thrown at src/main/java/com/thealgorithms/maths/FibonacciNumberGoldenRation.java:46

     * Reducing the limit to 70 due to potential floating-point arithmetic errors
     * that may result in incorrect results for larger inputs.
     */
    public static final int MAX_ARG = 70;

    /**
     * Calculates the nth Fibonacci number using Binet's formula.
     *
     * @param n The index of the Fibonacci number to calculate.
     * @return The nth Fibonacci number as a long.
     * @throws IllegalArgumentException if the input 'n' is negative or exceeds the range of a long data type.
     */
    public static long compute(int n) {
        if (n < 0) {
            throw new IllegalArgumentException("Input 'n' must be a non-negative integer.");
        }

        if (n > MAX_ARG) {
            throw new IllegalArgumentException("Input 'n' is too big to give accurate result.");
        }

        if (n <= 1) {
            return n;
        }

        // Calculate the nth Fibonacci number using the golden ratio formula
        final double sqrt5 = Math.sqrt(5);
        final double phi = (1 + sqrt5) / 2;
        final double psi = (1 - sqrt5) / 2;
        final double result = (Math.pow(phi, n) - Math.pow(psi, n)) / sqrt5;

        // Round to the nearest integer and return as a long
        return Math.round(result);
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. 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)).
  2. If you must use this class, cap n at 70 before calling compute().
  3. If you only need values up to 92 (the long overflow boundary) but with exactness, switch to an iterative BigInteger-based approach.

Example fix

// before
long fib = FibonacciNumberGoldenRation.compute(85);

// after — use exact iterative method for n > 70
long fib = (n <= 70)
    ? FibonacciNumberGoldenRation.compute(n)
    : FibonacciLoop.fibonacciNumber(n);
Defensive patterns

Strategy: validation

Validate before calling

if (n < 0 || n > FibonacciNumberGoldenRation.MAX_ARG) {
    throw new IllegalArgumentException("n must be in [0, " + FibonacciNumberGoldenRation.MAX_ARG + "]");
}
long fib = FibonacciNumberGoldenRation.compute(n);

Try / catch

try {
    long fib = FibonacciNumberGoldenRation.compute(n);
} catch (IllegalArgumentException e) {
    // n is negative or > 70; use an exact method for large n
    fib = FibonacciLoop.fibonacciNumber(n);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/e2992b8b2f97cbad. Report an issue: GitHub.