{"record":{"id":"190b7fcce83f5578","repo":"TheAlgorithms/Java","slug":"exponent-must-be-non-negative","errorCode":null,"errorMessage":"Exponent must be non-negative.","messagePattern":"Exponent must be non-negative\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/Pow.java","lineNumber":28,"sourceCode":"public final class Pow {\n    private Pow() {\n    }\n\n    /**\n     * Computes the value of the base raised to the power of the exponent.\n     * <p>\n     * The method calculates {@code a}<sup>{@code b}</sup> by iteratively multiplying the base {@code a} with itself {@code b} times.\n     * If the exponent {@code b} is negative, an {@code IllegalArgumentException} is thrown.\n     * </p>\n     *\n     * @param a the base of the exponentiation. Must be a non-negative integer.\n     * @param b the exponent to which the base {@code a} is raised. Must be a non-negative integer.\n     * @return the result of {@code a}<sup>{@code b}</sup> as a {@code long}.\n     * @throws IllegalArgumentException if {@code b} is negative.\n     */\n    public static long pow(int a, int b) {\n        if (b < 0) {\n            throw new IllegalArgumentException(\"Exponent must be non-negative.\");\n        }\n        long result = 1;\n        for (int i = 1; i <= b; i++) {\n            result *= a;\n        }\n        return result;\n    }\n}\n","sourceCodeStart":10,"sourceCodeEnd":37,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/Pow.java#L10-L37","documentation":"Thrown by Pow.pow(int a, int b) when the exponent b is negative. This implementation computes a^b by iterative multiplication (b times), which cannot represent a negative exponent (which would yield a fractional result). The guard rejects negatives before the loop to avoid an incorrect result of 1.","triggerScenarios":"Calling pow(a, -1) or any pow(a, b) where b < 0.","commonSituations":"Exponent computed from user input or a subtraction that can go negative; assumption that pow handles reciprocals; passing a loop counter that underflows; misreading the contract and expecting Math.pow semantics.","solutions":["If you need negative exponents, use Math.pow(a, b) (returns double) or implement a reciprocal variant.","Validate b >= 0 at the caller and reject or clamp before calling pow.","Fix upstream arithmetic so only non-negative exponents reach this call."],"exampleFix":"// before\nlong r = Pow.pow(base, exp);\n\n// after\nlong r = exp < 0 ? (long) Math.pow(base, exp) : Pow.pow(base, exp);","handlingStrategy":"validation","validationCode":"if (b < 0) {\n    // use double-precision power for negative exponents\n    double r = Math.pow(a, b);\n} else {\n    long r = Pow.pow(a, b);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate exponent is non-negative before calling this integer pow.","Use Math.pow for fractional or negative exponents.","Document at the call site whether negative exponents are errors or to be redirected."],"tags":["math","exponentiation","invalid-argument","negative-exponent"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}