TheAlgorithms/C-Sharp · error · ArgumentException
is not a positive integer
Error message
{0} is not a positive integer What it means
ModularPow computes (b^e) mod m using a naive loop; the modulus must be a positive integer because modular exponentiation is undefined for m <= 0 (and division by zero would occur for m = 0). The library throws ArgumentException with a formatted message naming the modulus. Base and exponent are not range-checked by this guard.
Solutions
- Ensure the modulus m is a positive integer (m >= 1) before calling.
- Check argument order: the modulus is the third parameter.
- If a zero modulus is legitimate in your domain, guard with a special case (result defined as 0) instead of calling.
Example fix
// before var r = ModularPow(baseVal, exp, mod); // mod may be 0 // after if (mod <= 0) throw new ArgumentOutOfRangeException(nameof(mod)); var r = ModularPow(baseVal, exp, mod);
Defensive patterns
Strategy: validation
Validate before calling
if (m <= 0) throw new ArgumentOutOfRangeException(nameof(m), "Modulus must be a positive integer"); var r = ModularPow(b, e, m);
Try / catch
try { var r = ModularPow(b, e, m); }
catch (ArgumentException ex) when (ex.Message.EndsWith("is not a positive integer")) { /* supply m >= 1 or define domain result */ } Prevention
- Verify argument order (base, exponent, modulus) before calling
- Default modulus config values to a valid positive constant, not 0
- Watch signed/unsigned conversions that can turn a modulus negative
When it happens
Trigger: Calling ModularPow(b, e, m) with m <= 0, e.g. ModularPow(2, 10, 0) or ModularPow(2, 10, -5).
Common situations: A modulus read from config defaulting to 0, an unsigned/signed conversion bug yielding negative m, or mistakenly passing the exponent as the third argument.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Invalid parameter settings for Ascon Hash
- Cash flows list cannot be empty
- The parameters 'listOfAs' and 'listOfNs' must not be null…
- The value for some n_i is smaller than or equal to 1.
- The value for some a_i is smaller than 0.
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/c5a0958b4e7b51f1.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Numeric/ModularExponentiation.cs:30
/// </summary>
/// <param name="b">Base.</param>
/// <param name="e">Exponent.</param>
/// <param name="m">Modulus.</param>
/// <returns>Modular Exponential.</returns>
public int ModularPow(int b, int e, int m)
{
// initialize result in variable res
int res = 1;
if (m == 1)
{
// 1 divides every number
return 0;
}
if (m <= 0)
{
// exponential not defined in this case
throw new ArgumentException(string.Format("{0} is not a positive integer", m));
}
for (int i = 0; i < e; i++)
{
res = (res * b) % m;
}
return res;
}
}
View on GitHub (pinned to 96e2905cab)