TheAlgorithms/Java · error · IllegalArgumentException

xEnd should be greater than xStart

Error message

xEnd should be greater than xStart

What it means

Thrown by EulerMethod.eulerFull when xStart >= xEnd. eulerFull integrates forward from xStart to xEnd in steps of stepSize; if the start is not strictly before the end, the while(xCurrent < xEnd) loop would never execute (or run zero times) and the integration direction would be ill-defined. The library treats a non-increasing interval as invalid rather than returning a single-point result.

Source

Thrown at src/main/java/com/thealgorithms/maths/EulerMethod.java:79

        }
        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) {
            throw new IllegalArgumentException("xEnd should be greater than xStart");
        }
        if (stepSize <= 0) {
            throw new IllegalArgumentException("stepSize should be greater than zero");
        }

        ArrayList<double[]> points = new ArrayList<double[]>();
        double[] firstPoint = {xStart, yStart};
        points.add(firstPoint);
        double yCurrent = yStart;
        double xCurrent = xStart;

        while (xCurrent < xEnd) {
            // Euler's method for next step
            yCurrent = eulerStep(xCurrent, stepSize, yCurrent, differentialEquation);
            xCurrent += stepSize;
            double[] point = {xCurrent, yCurrent};
            points.add(point);
        }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure xEnd > xStart, e.g. eulerFull(0.0, 1.0, 0.1, y0, f).
  2. If you need backward integration, swap the bounds and negate the step, or use a method that supports it.
  3. Validate the interval at the caller and surface a clear message if inverted.

Example fix

// before
EulerMethod.eulerFull(1.0, 1.0, 0.1, y0, f); // xStart == xEnd

// after
EulerMethod.eulerFull(0.0, 1.0, 0.1, y0, f);
Defensive patterns

Strategy: validation

Validate before calling

if (!(xEnd > xStart)) {
    throw new IllegalArgumentException("require xEnd > xStart");
}
EulerMethod.eulerFull(xStart, xEnd, stepSize, yStart, f);

Prevention

When it happens

Trigger: Calling eulerFull with xStart equal to xEnd, xStart greater than xEnd (e.g. integrating backward), or with bounds derived from input where the order is reversed.

Common situations: Swapped start/end arguments; bounds read from a config where the upper bound was defaulted to 0; integrating over a degenerate interval; sign error in computing the end point.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/625beb35d74291fc. Report an issue: GitHub.