{"record":{"id":"c75337fdbe59c18a","repo":"TheAlgorithms/Java","slug":"modulus-must-be-positive","errorCode":null,"errorMessage":"Modulus must be positive.","messagePattern":"Modulus must be positive\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/FastExponentiation.java","lineNumber":44,"sourceCode":"    }\n\n    /**\n     * Performs fast exponentiation to calculate (base^exp) % mod using the method\n     * 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","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/FastExponentiation.java#L26-L62","documentation":"Thrown by FastExponentiation.fastExponentiation when mod <= 0. Modular exponentiation (base^exp) % mod requires a positive modulus; a non-positive modulus makes the `% mod` operation undefined or meaningless and the algorithm's base = base % mod reduction would behave incorrectly. The guard runs before any reduction, so reaching the loop confirms mod is positive.","triggerScenarios":"Calling fastExponentiation(base, exp, 0), with a negative mod, or with mod parsed from config that defaulted to 0. Common in cryptographic contexts where the modulus must be a positive prime or composite.","commonSituations":"Modulus read from a missing config key (parses to 0); modulus computed as a difference that underflowed; passing a sentinel 0; off-by-one in prime generation yielding 0.","solutions":["Pass a positive modulus such as fastExponentiation(base, exp, 1000000007).","Validate mod > 0 at the caller before invoking.","If mod comes from input, default to a known positive constant when absent."],"exampleFix":"// before\nlong r = FastExponentiation.fastExponentiation(2, 10, 0);\n\n// after\nlong r = FastExponentiation.fastExponentiation(2, 10, 1000000007L);","handlingStrategy":"validation","validationCode":"if (mod <= 0) {\n    throw new IllegalArgumentException(\"modulus must be > 0\");\n}\nFastExponentiation.fastExponentiation(base, exp, mod);","typeGuard":"static boolean isPositiveModulus(long mod) { return mod > 0; }","tryCatchPattern":null,"preventionTips":["Default the modulus to a known positive constant (e.g. 1_000_000_007L) when config is absent.","Validate modulus at startup for cryptographic pipelines.","Never pass a sentinel 0 as the modulus."],"tags":["validation","number-theory","modular-arithmetic","cryptographic","precondition"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}