TheAlgorithms/Java · error · ArithmeticException

Negative exponent is not supported.

Error message

Negative exponent is not supported.

What it means

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.

Source

Thrown at src/main/java/com/thealgorithms/maths/FastExponentiation.java:48

     * of exponentiation by squaring.
     *
     * <p>This method efficiently computes the result by squaring the base and halving
     * the exponent at each step. It multiplies the base to the result when the exponent is odd.
     *
     * @param base the base number to be raised to the power of exp
     * @param exp the exponent to which the base is raised
     * @param mod the modulus to ensure the result does not overflow
     * @return (base^exp) % mod
     * @throws IllegalArgumentException if the modulus is less than or equal to 0
     * @throws ArithmeticException if the exponent is negative (not supported in this implementation)
     */
    public static long fastExponentiation(long base, long exp, long mod) {
        if (mod <= 0) {
            throw new IllegalArgumentException("Modulus must be positive.");
        }

        if (exp < 0) {
            throw new ArithmeticException("Negative exponent is not supported.");
        }

        long result = 1;
        base = base % mod; // Take the modulus of the base to handle large base values

        // Fast exponentiation by squaring algorithm
        while (exp > 0) {
            // If exp is odd, multiply the base to the result
            if ((exp & 1) == 1) { // exp & 1 checks if exp is odd
                result = result * base % mod;
            }
            // Square the base and halve the exponent
            base = base * base % mod; // base^2 % mod to avoid overflow
            exp >>= 1; // Right shift exp to divide it by 2
        }

        return result;
    }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass a non-negative exponent (>= 0); fastExponentiation(base, 0, mod) returns 1.
  2. 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.
  3. Validate and clamp exp to >= 0, or compute the inverse explicitly.

Example fix

// before
long inv = FastExponentiation.fastExponentiation(a, -1, p); // throws

// after
// modular inverse via Fermat for prime p:
long inv = FastExponentiation.fastExponentiation(a, p - 2, p);
Defensive patterns

Strategy: validation

Validate before calling

if (exp < 0) {
    throw new IllegalArgumentException("exponent must be >= 0; use modular inverse for negative");
}
FastExponentiation.fastExponentiation(base, exp, mod);

Try / catch

try {
    long r = FastExponentiation.fastExponentiation(base, exp, mod);
} catch (ArithmeticException e) {
    // handle unsupported negative exponent, e.g. fall back to modular inverse
}

Prevention

When it happens

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

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

Related errors


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