TheAlgorithms/C-Sharp · error · ArgumentOutOfRangeException

should be greater than zero

Error message

{nameof(stepSize)} should be greater than zero

What it means

Factorial.Calculate(inputNum) computes n! using BigInteger arithmetic and only defines the factorial for non-negative integers. A negative input throws ArgumentException with the terse message 'Only for num >= 0'. The comparison is done via BigInteger.Compare against BigInteger.Zero after converting the int input.

Solutions

  1. Check inputNum >= 0 before calling and handle negatives in your own logic (return 0, throw your own clearer error, etc.).
  2. If you need the gamma function for negative/non-integer arguments, use a math library that provides Gamma, not this factorial method.
  3. Validate user input at the boundary so negative values never reach the calculation.

Example fix

// before
var f = Factorial.Calculate(n); // throws when n < 0
// after
var f = n >= 0 ? Factorial.Calculate(n) : BigInteger.Zero;
Defensive patterns

Strategy: validation

Validate before calling

if (n < 0)
    return BigInteger.Zero; // or throw a domain-specific error
var f = Factorial.Calculate(n);

Try / catch

try
{
    f = Factorial.Calculate(n);
}
catch (ArgumentException ex) when (ex.Message == "Only for num >= 0")
{
    f = BigInteger.Zero;
}

Prevention

When it happens

Trigger: Calling Calculate with a negative int, e.g. Factorial.Calculate(-1); passing a value that became negative through subtraction, user input, or a recursive formula base case handled incorrectly.

Common situations: Combinatorics formulas where intermediate terms can go negative (e.g. permutations with invalid arguments); unvalidated user input for n; gamma-function-style expectations that factorial extends to negatives (it does not here).

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


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/1717ef661fd92b66. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Numeric/EulerMethod.cs:42

    /// <param name="yDerivative">The right hand side of the differential equation.</param>
    /// <returns>The solution of the Cauchy problem.</returns>
    public static List<double[]> EulerFull(
        double xStart,
        double xEnd,
        double stepSize,
        double yStart,
        Func<double, double, double> yDerivative)
    {
        if (xStart >= xEnd)
        {
            throw new ArgumentOutOfRangeException(
                nameof(xEnd),
                $"{nameof(xEnd)} should be greater than {nameof(xStart)}");
        }

        if (stepSize <= 0)
        {
            throw new ArgumentOutOfRangeException(
                nameof(stepSize),
                $"{nameof(stepSize)} should be greater than zero");
        }

        List<double[]> points = [];
        double[] firstPoint = [xStart, yStart];
        points.Add(firstPoint);
        var yCurrent = yStart;
        var xCurrent = xStart;

        while (xCurrent < xEnd)
        {
            yCurrent = EulerStep(xCurrent, stepSize, yCurrent, yDerivative);
            xCurrent += stepSize;
            double[] point = [xCurrent, yCurrent];
            points.Add(point);
        }

View on GitHub (pinned to 96e2905cab)