TheAlgorithms/Java · error · IllegalArgumentException
stepSize should be greater than zero
Error message
stepSize should be greater than zero
What it means
Thrown by EulerMethod.eulerStep when stepSize <= 0. eulerStep computes a single Euler integration step y_next = yCurrent + stepSize * f(x,y); a non-positive step would move backward or not at all, violating the method's contract as a forward integrator. The guard runs before the function evaluation so no computation is wasted on an invalid step.
Source
Thrown at src/main/java/com/thealgorithms/maths/EulerMethod.java:60
BiFunction<Double, Double, Double> exampleEquation3 = (x, y) -> x + y + x * y;
ArrayList<double[]> points3 = eulerFull(0, 0.1, 0.025, 1, exampleEquation3);
assert points3.get(points3.size() - 1)[1] == 1.1116729841674804;
points3.forEach(point -> System.out.printf("x: %1$f; y: %2$f%n", point[0], point[1]));
}
/**
* calculates the next y-value based on the current value of x, y and the
* stepSize the console.
*
* @param xCurrent Current x-value.
* @param stepSize Step-size on the x-axis.
* @param yCurrent Current y-value.
* @param differentialEquation The differential equation to be solved.
* @return The next y-value.
*/
public static double eulerStep(double xCurrent, double stepSize, double yCurrent, BiFunction<Double, Double, Double> differentialEquation) {
if (stepSize <= 0) {
throw new IllegalArgumentException("stepSize should be greater than zero");
}
return yCurrent + stepSize * differentialEquation.apply(xCurrent, yCurrent);
}
/**
* Loops through all the steps until xEnd is reached, adds a point for each
* step and then returns all the points
*
* @param xStart First x-value.
* @param xEnd Last x-value.
* @param stepSize Step-size on the x-axis.
* @param yStart First y-value.
* @param differentialEquation The differential equation to be solved.
* @return The points constituting the solution of the differential
* equation.
*/
public static ArrayList<double[]> eulerFull(double xStart, double xEnd, double stepSize, double yStart, BiFunction<Double, Double, Double> differentialEquation) {
if (xStart >= xEnd) {View on GitHub (pinned to fdfb9a395b)
Solutions
- Pass a positive stepSize such as 0.1 or 0.01.
- If computing stepSize adaptively, clamp it to a positive minimum.
- Validate config-derived values before the call.
Example fix
// before double yNext = EulerMethod.eulerStep(x, 0, y, f); // after double yNext = EulerMethod.eulerStep(x, 0.1, y, f);
Defensive patterns
Strategy: validation
Validate before calling
if (!(stepSize > 0)) {
throw new IllegalArgumentException("stepSize must be > 0");
}
EulerMethod.eulerStep(x, stepSize, y, f); Prevention
- Clamp adaptively-computed steps to a positive minimum.
- Default config stepSize to a positive constant.
- Guard against numSteps == 0 when deriving stepSize by division.
When it happens
Trigger: Calling eulerStep with stepSize = 0, a negative value, or a stepSize parsed from config that defaulted to 0. Because eulerStep is also called internally by eulerFull, this can surface indirectly if eulerFull's own guard is bypassed or if eulerStep is used standalone.
Common situations: Adaptive-step code that computes a step size which collapses to 0; config file missing the stepSize key and parsing to 0; reusing a step variable that was zeroed.
Related errors
- xEnd should be greater than xStart
- Tolerance must be positive.
- Input 'n' must be a non-negative integer.
- Table is empty; cannot find keys.
- Numbers array cannot be empty or null
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/df280a96f82c7aaf.
Report an issue: GitHub.