{"record":{"id":"defba44b9e44b6fe","repo":"TheAlgorithms/C-Sharp","slug":"nameof-stepsize-should-be-greater-than-zero-rungekuttamethod","errorCode":null,"errorMessage":"{nameof(stepSize)} should be greater than zero","messagePattern":"(.+?) should be greater than zero","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Algorithms/Numeric/RungeKuttaMethod.cs","lineNumber":37,"sourceCode":"    /// <param name=\"function\">The right hand side of the differential equation.</param>\n    /// <returns>The solution of the Cauchy problem.</returns>\n    public static List<double[]> ClassicRungeKuttaMethod(\n        double xStart,\n        double xEnd,\n        double stepSize,\n        double yStart,\n        Func<double, double, double> function)\n    {\n        if (xStart >= xEnd)\n        {\n            throw new ArgumentOutOfRangeException(\n                nameof(xEnd),\n                $\"{nameof(xEnd)} should be greater than {nameof(xStart)}\");\n        }\n\n        if (stepSize <= 0)\n        {\n            throw new ArgumentOutOfRangeException(\n                nameof(stepSize),\n                $\"{nameof(stepSize)} should be greater than zero\");\n        }\n\n        List<double[]> points = [];\n        double[] firstPoint = [xStart, yStart];\n        points.Add(firstPoint);\n\n        var yCurrent = yStart;\n        var xCurrent = xStart;\n\n        while (xCurrent < xEnd)\n        {\n            var k1 = function(xCurrent, yCurrent);\n            var k2 = function(xCurrent + 0.5 * stepSize, yCurrent + 0.5 * stepSize * k1);\n            var k3 = function(xCurrent + 0.5 * stepSize, yCurrent + 0.5 * stepSize * k2);\n            var k4 = function(xCurrent + stepSize, yCurrent + stepSize * k3);\n","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Numeric/RungeKuttaMethod.cs#L19-L55","documentation":"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.","triggerScenarios":"Calling ClassicRungeKuttaMethod with stepSize <= 0, e.g. 0 from a default config or a negative value from a sign error.","commonSituations":"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.","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."],"exampleFix":"// before\nvar points = solver.ClassicRungeKuttaMethod(0, 1, 1, f, stepSize: 0);\n// after\nvar h = Math.Max(1e-6, configuredStep);\nvar points = solver.ClassicRungeKuttaMethod(0, 1, 1, f, stepSize: h);","handlingStrategy":"validation","validationCode":"if (stepSize <= 0) throw new ArgumentOutOfRangeException(nameof(stepSize), \"stepSize must be > 0\");\nvar points = ClassicRungeKuttaMethod(xStart, xEnd, stepSize, yStart, f);","typeGuard":null,"tryCatchPattern":"try { var pts = ClassicRungeKuttaMethod(x0, x1, h, y0, f); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"stepSize\") { /* clamp h to a small positive value */ }","preventionTips":["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"],"tags":["argument-out-of-range","ode-solver","csharp"],"backgroundTag":"argument-out-of-range","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}