TheAlgorithms/Java · error · IllegalArgumentException

Tolerance must be positive.

Error message

Tolerance must be positive.

What it means

Thrown by ChebyshevIteration.validateInputs when the tolerance argument is <= 0. Tolerance is the convergence threshold for this iterative linear-system solver: the iteration stops once the residual norm falls below it. A non-positive tolerance makes the stopping criterion meaningless (<= 0 would either never converge or accept any result), so the library rejects it up front. The check runs after dimension, square-matrix, eigenvalue, and iteration-count guards, so passing it confirms the rest of the precondition block was satisfied.

Source

Thrown at src/main/java/com/thealgorithms/maths/ChebyshevIteration.java:114

            throw new IllegalArgumentException("Matrix A must be square.");
        }
        if (n != b.length) {
            throw new IllegalArgumentException("Matrix A and vector b dimensions do not match.");
        }
        if (n != x0.length) {
            throw new IllegalArgumentException("Matrix A and vector x0 dimensions do not match.");
        }
        if (minEigenvalue <= 0) {
            throw new IllegalArgumentException("Smallest eigenvalue must be positive (matrix must be positive-definite).");
        }
        if (maxEigenvalue <= minEigenvalue) {
            throw new IllegalArgumentException("Max eigenvalue must be strictly greater than min eigenvalue.");
        }
        if (maxIterations <= 0) {
            throw new IllegalArgumentException("Max iterations must be positive.");
        }
        if (tolerance <= 0) {
            throw new IllegalArgumentException("Tolerance must be positive.");
        }
    }

    // --- Vector/Matrix Helper Methods ---
    /**
     * Computes the product of a matrix A and a vector v (Av).
     */
    private static double[] matrixVectorMultiply(double[][] a, double[] v) {
        int n = a.length;
        double[] result = new double[n];
        for (int i = 0; i < n; i++) {
            double sum = 0;
            for (int j = 0; j < n; j++) {
                sum += a[i][j] * v[j];
            }
            result[i] = sum;
        }
        return result;

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass an explicit positive tolerance such as 1e-10 or 1e-6.
  2. If tolerance comes from config, default it to a positive value when the parsed value is not greater than zero: tolerance = (parsed > 0) ? parsed : 1e-10.
  3. Validate at the application boundary and surface a clear error to the end user before calling the solver.
  4. If you genuinely want a fixed-iteration run, still supply a tiny positive tolerance and rely on maxIterations as the bound.

Example fix

// before
ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, 0);

// after
ChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, 1e-10);
Defensive patterns

Strategy: validation

Validate before calling

if (!(tolerance > 0)) {
    throw new IllegalArgumentException("tolerance must be > 0, got " + tolerance);
}
// then call the solver

Prevention

When it happens

Trigger: Calling the Chebyshev solver with tolerance = 0, a negative tolerance (e.g. -1e-6), or leaving a default zero-valued double field uninitialised before the call. Any caller that derives tolerance from user input or config without clamping it to a positive value triggers this.

Common situations: Reading a tolerance from a properties file that is missing or misparsed to 0; using a UI text field whose default is empty and parses to 0.0; passing a relative tolerance computed as a difference that underflowed to 0; reusing a configuration object across solvers where a different solver allowed tolerance=0 as a sentinel.

Related errors


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