TheAlgorithms/Java · error · IllegalArgumentException

Input 'n' must be a non-negative integer.

Error message

Input 'n' must be a non-negative integer.

What it means

Thrown by FibonacciNumberGoldenRation.compute when n < 0. This method uses Binet's formula (closed-form via the golden ratio) which is defined for non-negative indices; additionally the class caps n at MAX_ARG (70) because double-precision arithmetic overflows beyond that. The negativity guard is the first of two checks (the second rejects n > MAX_ARG), so hitting it means the index is negative.

Source

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

    }

    /**
     * Compute the limit for 'n' that fits in a long data type.
     * 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. Pass a non-negative int n in [0, 70] such as compute(10).
  2. Validate 0 <= n <= MAX_ARG at the caller before invoking.
  3. If you need indices beyond 70, use FibonacciLoop or the matrix-exponentiation variant for arbitrary precision.

Example fix

// before
long f = FibonacciNumberGoldenRation.compute(-1);

// after
long f = FibonacciNumberGoldenRation.compute(10);
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 + "]");
}
FibonacciNumberGoldenRation.compute(n);

Type guard

static boolean isValidGoldenRatioArg(int n) {
    return n >= 0 && n <= FibonacciNumberGoldenRation.MAX_ARG;
}

Prevention

When it happens

Trigger: Calling compute(-5) or any negative n. Common when n is derived from user input or subtraction. The guard fires before the MAX_ARG check and before the Binet computation.

Common situations: User input accepting negative numbers; index from a subtraction that underflows; default int value of 0 is fine (compute(0) returns 0) but negative loops are not; off-by-one in decrementing.

Related errors


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