TheAlgorithms/C-Sharp · error · ArgumentOutOfRangeException

should be greater than

Error message

{nameof(xEnd)} should be greater than {nameof(xStart)}

What it means

EulerMethod.EulerFull throws ArgumentOutOfRangeException when stepSize <= 0. The explicit Euler method advances the solution by fixed positive steps; a zero or negative step size would cause an infinite loop or backward integration the method does not support.

Solutions

  1. Pass a strictly positive stepSize: validate stepSize > 0 before calling.
  2. If deriving stepSize from (xEnd - xStart) / steps, guard steps >= 1 and xEnd > xStart first.
  3. Clamp or reject non-positive configured step sizes at config-load time with a clear message.

Example fix

// before
var h = (xEnd - xStart) / steps; // steps == 0 => h = 0 => throws
var points = EulerMethod.EulerFull(xStart, xEnd, h, y0, f);
// after
if (steps < 1) throw new ArgumentException("steps must be >= 1");
var h = (xEnd - xStart) / steps;
var points = EulerMethod.EulerFull(xStart, xEnd, h, y0, f);
Defensive patterns

Strategy: validation

Validate before calling

if (stepSize <= 0)
    throw new ArgumentException("stepSize must be positive");
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 == "stepSize")
{
    // use a default positive step or surface config error
}

Prevention

When it happens

Trigger: Calling EulerFull with stepSize = 0 or a negative value; a step size computed as (xEnd - xStart) / steps where steps was 0 or the interval was inverted, yielding zero/negative results.

Common situations: Step count computed from an empty or reversed time range; configuration where stepSize defaults to 0 before being set; sign error when converting a 'dt' parameter from another library that allows negative steps.

Related errors


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

Appendix: source

Thrown at Algorithms/Numeric/EulerMethod.cs:35

    ///     Loops through all the steps until xEnd is reached, adds a point for each step and then
    ///     returns all the points.
    /// </summary>
    /// <param name="xStart">Initial conditions x-value.</param>
    /// <param name="xEnd">Last x-value.</param>
    /// <param name="stepSize">Step-size on the x-axis.</param>
    /// <param name="yStart">Initial conditions y-value.</param>
    /// <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)

View on GitHub (pinned to 96e2905cab)