TheAlgorithms/C-Sharp · error · ArgumentException

Double factorial is only defined for non-negative integers…

Error message

Double factorial is only defined for non-negative integers (num >= 0).

What it means

EulerMethod.EulerFull integrates an ODE with the explicit Euler method from xStart to xEnd. It throws ArgumentOutOfRangeException when xStart >= xEnd because there would be no forward interval to integrate over. The message uses nameof interpolation and reads '{xEnd} should be greater than {xStart}'.

Solutions

  1. Ensure xEnd > xStart before calling; normalize with Math.Min/Math.Max if the input order is not guaranteed.
  2. For backward integration, negate the derivative function and integrate forward from the later bound.
  3. Validate the configured time range at the source (config parsing, UI input) rather than at the numeric call.

Example fix

// before
var points = EulerMethod.EulerFull(tEnd, tStart, h, y0, f); // tEnd <= tStart throws
// after
if (tEnd <= tStart) throw new ArgumentException("tEnd must be after tStart");
var points = EulerMethod.EulerFull(tStart, tEnd, h, y0, f);
Defensive patterns

Strategy: validation

Validate before calling

if (xEnd <= xStart)
    throw new ArgumentException("xEnd must be greater than xStart");
var points = EulerMethod.EulerFull(xStart, xEnd, stepSize, yStart, yDerivative);

Try / catch

try
{
    var points = EulerMethod.EulerFull(xStart, xEnd, h, y0, f);
}
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "xEnd")
{
    // handle reversed/degenerate interval
}

Prevention

When it happens

Trigger: Calling EulerFull with xEnd <= xStart, e.g. EulerFull(1.0, 1.0, ...) (equal bounds) or reversed bounds EulerFull(2.0, 0.0, ...); computing xEnd from a formula that lands before or on xStart.

Common situations: Backward integration attempted by swapping bounds (the method only integrates forward); unit tests with degenerate equal endpoints; time range configured from user input where start and end were entered in the wrong order.

Related errors


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

Appendix: source

Thrown at Algorithms/Numeric/DoubleFactorial.cs:22

/// <summary>
///     The double factorial of a positive integer n, denoted by n!!,
///     is the product of all integers from 1 up to n that have the same parity (odd or even) as n.
///     E.g., 5!! = 5 * 3 * 1 = 15, and 6!! = 6 * 4 * 2 = 48.
/// </summary>
public static class DoubleFactorial
{
    /// <summary>
    ///     Calculates the double factorial of a non-negative integer number.
    /// </summary>
    /// <param name="inputNum">Non-negative integer input number.</param>
    /// <returns>Double factorial of the integer input number.</returns>
    public static BigInteger Calculate(int inputNum)
    {
        // Don't calculate double factorial if input is a negative number.
        if (inputNum < 0)
        {
            throw new ArgumentException("Double factorial is only defined for non-negative integers (num >= 0).");
        }

        // Base cases: 0!! = 1 and 1!! = 1
        if (inputNum <= 1)
        {
            return BigInteger.One;
        }

        // Initialize result.
        BigInteger result = BigInteger.One;

        // Start the iteration from the input number and step down by 2.
        // This handles both odd (n, n-2, ..., 3, 1) and even (n, n-2, ..., 4, 2) cases naturally.
        BigInteger current = inputNum;

        while (current > BigInteger.Zero)
        {
            result *= current;

View on GitHub (pinned to 96e2905cab)