{"record":{"id":"cdd860dd7058fb08","repo":"TheAlgorithms/Java","slug":"input-n-must-be-a-non-negative-integer-cdd860","errorCode":null,"errorMessage":"Input 'n' must be a non-negative integer.","messagePattern":"Input 'n' must be a non-negative integer\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/FibonacciNumberGoldenRation.java","lineNumber":42,"sourceCode":"    }\n\n    /**\n     * Compute the limit for 'n' that fits in a long data type.\n     * 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);","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/FibonacciNumberGoldenRation.java#L24-L60","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a non-negative int n in [0, 70] such as compute(10).","Validate 0 <= n <= MAX_ARG at the caller before invoking.","If you need indices beyond 70, use FibonacciLoop or the matrix-exponentiation variant for arbitrary precision."],"exampleFix":"// before\nlong f = FibonacciNumberGoldenRation.compute(-1);\n\n// after\nlong f = FibonacciNumberGoldenRation.compute(10);","handlingStrategy":"validation","validationCode":"if (n < 0 || n > FibonacciNumberGoldenRation.MAX_ARG) {\n    throw new IllegalArgumentException(\"n must be in [0, \" + FibonacciNumberGoldenRation.MAX_ARG + \"]\");\n}\nFibonacciNumberGoldenRation.compute(n);","typeGuard":"static boolean isValidGoldenRatioArg(int n) {\n    return n >= 0 && n <= FibonacciNumberGoldenRation.MAX_ARG;\n}","tryCatchPattern":null,"preventionTips":["Bound n to [0, 70] before calling this Binet-based method.","For indices beyond 70, switch to FibonacciLoop or matrix exponentiation.","Validate parsed indices at the boundary."],"tags":["validation","sequences","precondition","numerical-methods"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}