{"record":{"id":"ef04246cc0063876","repo":"TheAlgorithms/Java","slug":"negative-exponent-is-not-supported","errorCode":null,"errorMessage":"Negative exponent is not supported.","messagePattern":"Negative exponent is not supported\\.","errorType":"validation","errorClass":"ArithmeticException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/FastExponentiation.java","lineNumber":48,"sourceCode":"     * of exponentiation by squaring.\n     *\n     * <p>This method efficiently computes the result by squaring the base and halving\n     * the exponent at each step. It multiplies the base to the result when the exponent is odd.\n     *\n     * @param base the base number to be raised to the power of exp\n     * @param exp the exponent to which the base is raised\n     * @param mod the modulus to ensure the result does not overflow\n     * @return (base^exp) % mod\n     * @throws IllegalArgumentException if the modulus is less than or equal to 0\n     * @throws ArithmeticException if the exponent is negative (not supported in this implementation)\n     */\n    public static long fastExponentiation(long base, long exp, long mod) {\n        if (mod <= 0) {\n            throw new IllegalArgumentException(\"Modulus must be positive.\");\n        }\n\n        if (exp < 0) {\n            throw new ArithmeticException(\"Negative exponent is not supported.\");\n        }\n\n        long result = 1;\n        base = base % mod; // Take the modulus of the base to handle large base values\n\n        // Fast exponentiation by squaring algorithm\n        while (exp > 0) {\n            // If exp is odd, multiply the base to the result\n            if ((exp & 1) == 1) { // exp & 1 checks if exp is odd\n                result = result * base % mod;\n            }\n            // Square the base and halve the exponent\n            base = base * base % mod; // base^2 % mod to avoid overflow\n            exp >>= 1; // Right shift exp to divide it by 2\n        }\n\n        return result;\n    }","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/FastExponentiation.java#L30-L66","documentation":"Thrown by FastExponentiation.fastExponentiation when exp < 0. This is an ArithmeticException (not IllegalArgumentException), distinguishing 'unsupported operation' from 'bad argument'. The implementation uses exponentiation-by-squaring with a while(exp > 0) loop, which cannot represent negative exponents (those require modular inverse computation). The guard fires after the modulus check, so it confirms mod was valid.","triggerScenarios":"Calling fastExponentiation(base, -3, mod) with any negative exponent. Common when exp is derived from subtraction or parsed from input, especially in modular-inverse code that naively passes a negative power instead of computing the inverse.","commonSituations":"Code that confuses modular exponentiation with modular inverse (which needs exp = phi-1 or similar); exp read as a signed value that can be negative; subtraction producing a negative exponent.","solutions":["Pass a non-negative exponent (>= 0); fastExponentiation(base, 0, mod) returns 1.","If you need the modular inverse, compute it via Fermat's little theorem (exp = mod-2 for prime mod) or the extended Euclidean algorithm rather than a negative exponent.","Validate and clamp exp to >= 0, or compute the inverse explicitly."],"exampleFix":"// before\nlong inv = FastExponentiation.fastExponentiation(a, -1, p); // throws\n\n// after\n// modular inverse via Fermat for prime p:\nlong inv = FastExponentiation.fastExponentiation(a, p - 2, p);","handlingStrategy":"validation","validationCode":"if (exp < 0) {\n    throw new IllegalArgumentException(\"exponent must be >= 0; use modular inverse for negative\");\n}\nFastExponentiation.fastExponentiation(base, exp, mod);","typeGuard":null,"tryCatchPattern":"try {\n    long r = FastExponentiation.fastExponentiation(base, exp, mod);\n} catch (ArithmeticException e) {\n    // handle unsupported negative exponent, e.g. fall back to modular inverse\n}","preventionTips":["For modular inverse, use exp = p - 2 (Fermat) for prime p, not a negative exponent.","Validate exp >= 0 before calling.","Catch ArithmeticException distinctly from IllegalArgumentException to separate the two failure modes."],"tags":["arithmetic","number-theory","modular-arithmetic","cryptographic","unsupported-operation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}