TheAlgorithms/C-Sharp · error · ArgumentOutOfRangeException
should be greater than zero
Error message
{nameof(stepSize)} should be greater than zero What it means
ClassicRungeKuttaMethod requires a strictly positive stepSize to advance the RK4 integration; a zero or negative step produces no progress or wrong-direction integration, so it throws ArgumentOutOfRangeException naming stepSize. The message interpolates the parameter name.
Solutions
- Pass stepSize > 0 and (for this API) ensure xEnd > xStart so the integration proceeds forward.
- Validate/clamp the step from config before calling.
- If the interval is shorter than one step, reduce stepSize to fit the interval.
Example fix
// before var points = solver.ClassicRungeKuttaMethod(0, 1, 1, f, stepSize: 0); // after var h = Math.Max(1e-6, configuredStep); var points = solver.ClassicRungeKuttaMethod(0, 1, 1, f, stepSize: h);
Defensive patterns
Strategy: validation
Validate before calling
if (stepSize <= 0) throw new ArgumentOutOfRangeException(nameof(stepSize), "stepSize must be > 0"); var points = ClassicRungeKuttaMethod(xStart, xEnd, stepSize, yStart, f);
Try / catch
try { var pts = ClassicRungeKuttaMethod(x0, x1, h, y0, f); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "stepSize") { /* clamp h to a small positive value */ } Prevention
- Give step-size config entries a positive default and validate on load
- Compute h = (xEnd - xStart) / steps with steps >= 1 so h > 0
- Clamp h to a small epsilon rather than allowing 0
When it happens
Trigger: Calling ClassicRungeKuttaMethod with stepSize <= 0, e.g. 0 from a default config or a negative value from a sign error.
Common situations: A step size read from configuration that defaulted to 0, dividing a zero-length interval by a count to compute the step, or a sign flip when supporting backward integration manually.
Related errors
- should be greater than
- Invalid block size
- Not enough space in input array for padding
- Vertex indices must be within valid range.
- k must be at least 1.
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/defba44b9e44b6fe.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Numeric/RungeKuttaMethod.cs:37
/// <param name="function">The right hand side of the differential equation.</param>
/// <returns>The solution of the Cauchy problem.</returns>
public static List<double[]> ClassicRungeKuttaMethod(
double xStart,
double xEnd,
double stepSize,
double yStart,
Func<double, double, double> function)
{
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)
{
var k1 = function(xCurrent, yCurrent);
var k2 = function(xCurrent + 0.5 * stepSize, yCurrent + 0.5 * stepSize * k1);
var k3 = function(xCurrent + 0.5 * stepSize, yCurrent + 0.5 * stepSize * k2);
var k4 = function(xCurrent + stepSize, yCurrent + stepSize * k3);
View on GitHub (pinned to 96e2905cab)