{"record":{"id":"801adfea518e84d0","repo":"TheAlgorithms/Java","slug":"tolerance-must-be-positive","errorCode":null,"errorMessage":"Tolerance must be positive.","messagePattern":"Tolerance must be positive\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/ChebyshevIteration.java","lineNumber":114,"sourceCode":"            throw new IllegalArgumentException(\"Matrix A must be square.\");\n        }\n        if (n != b.length) {\n            throw new IllegalArgumentException(\"Matrix A and vector b dimensions do not match.\");\n        }\n        if (n != x0.length) {\n            throw new IllegalArgumentException(\"Matrix A and vector x0 dimensions do not match.\");\n        }\n        if (minEigenvalue <= 0) {\n            throw new IllegalArgumentException(\"Smallest eigenvalue must be positive (matrix must be positive-definite).\");\n        }\n        if (maxEigenvalue <= minEigenvalue) {\n            throw new IllegalArgumentException(\"Max eigenvalue must be strictly greater than min eigenvalue.\");\n        }\n        if (maxIterations <= 0) {\n            throw new IllegalArgumentException(\"Max iterations must be positive.\");\n        }\n        if (tolerance <= 0) {\n            throw new IllegalArgumentException(\"Tolerance must be positive.\");\n        }\n    }\n\n    // --- Vector/Matrix Helper Methods ---\n    /**\n     * Computes the product of a matrix A and a vector v (Av).\n     */\n    private static double[] matrixVectorMultiply(double[][] a, double[] v) {\n        int n = a.length;\n        double[] result = new double[n];\n        for (int i = 0; i < n; i++) {\n            double sum = 0;\n            for (int j = 0; j < n; j++) {\n                sum += a[i][j] * v[j];\n            }\n            result[i] = sum;\n        }\n        return result;","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/ChebyshevIteration.java#L96-L132","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass an explicit positive tolerance such as 1e-10 or 1e-6.","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.","Validate at the application boundary and surface a clear error to the end user before calling the solver.","If you genuinely want a fixed-iteration run, still supply a tiny positive tolerance and rely on maxIterations as the bound."],"exampleFix":"// before\nChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, 0);\n\n// after\nChebyshevIteration.solve(a, b, x0, minEig, maxEig, maxIter, 1e-10);","handlingStrategy":"validation","validationCode":"if (!(tolerance > 0)) {\n    throw new IllegalArgumentException(\"tolerance must be > 0, got \" + tolerance);\n}\n// then call the solver","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Default tolerance to a positive constant (1e-10) when config is missing or unparsable.","Treat tolerance as a required positive config field with a validation step at startup.","Never reuse a zero-initialised double field as the final tolerance without assigning it."],"tags":["validation","numerical-methods","linear-algebra","precondition"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}