{"record":{"id":"f6706db691d5cbec","repo":"TheAlgorithms/Java","slug":"acoeffs-get-0-must-not-be-zero","errorCode":null,"errorMessage":"aCoeffs.get(0) must not be zero","messagePattern":"aCoeffs\\.get\\(0\\) must not be zero","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/audiofilters/IIRFilter.java","lineNumber":54,"sourceCode":"        historyX = new double[order];\n        historyY = new double[order];\n    }\n\n    /**\n     * Set coefficients\n     *\n     * @param aCoeffs Denominator coefficients\n     * @param bCoeffs Numerator coefficients\n     * @throws IllegalArgumentException if {@code aCoeffs} or {@code bCoeffs} is\n     * not of size {@code order}, or if {@code aCoeffs[0]} is 0.0\n     */\n    public void setCoeffs(double[] aCoeffs, double[] bCoeffs) throws IllegalArgumentException {\n        if (aCoeffs.length != order) {\n            throw new IllegalArgumentException(\"aCoeffs must be of size \" + order + \", got \" + aCoeffs.length);\n        }\n\n        if (aCoeffs[0] == 0.0) {\n            throw new IllegalArgumentException(\"aCoeffs.get(0) must not be zero\");\n        }\n\n        if (bCoeffs.length != order) {\n            throw new IllegalArgumentException(\"bCoeffs must be of size \" + order + \", got \" + bCoeffs.length);\n        }\n\n        for (int i = 0; i < order; i++) {\n            coeffsA[i] = aCoeffs[i];\n            coeffsB[i] = bCoeffs[i];\n        }\n    }\n\n    /**\n     * Process a single sample\n     *\n     * @param sample the sample to process\n     * @return the processed sample\n     */","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/audiofilters/IIRFilter.java#L36-L72","documentation":"Thrown by IIRFilter.setCoeffs when aCoeffs[0] is exactly 0.0. In the difference equation, the processed sample is divided by coeffsA[0] (`(result + coeffsB[0]*sample) / coeffsA[0]`), so a zero leading denominator coefficient would produce division by zero or NaN. This guard rejects that degenerate case explicitly.","triggerScenarios":"Calling `setCoeffs(a, b)` where `a[0] == 0.0`. A normalized IIR denominator typically has a0 = 1.0; passing a raw (non-normalized) denominator whose first element is 0 triggers this.","commonSituations":"Forgetting to normalize the denominator so that a0 = 1; coefficients produced by a design routine that returned the denominator in a different orientation; accidentally zeroing the leading coefficient during array manipulation.","solutions":["Normalize the denominator array by dividing every element by a[0] so that a[0] becomes 1.0 (standard IIR form).","Verify the leading coefficient is non-zero before calling setCoeffs.","Re-check the coefficient source if a0 unexpectedly arrives as 0."],"exampleFix":"// before\nf.setCoeffs(new double[]{0.0, 0.5, 0.25}, b);  // throws\n\n// after\ndouble a0 = a[0];\ndouble[] aNorm = new double[a.length];\nfor (int i = 0; i < a.length; i++) aNorm[i] = a[i] / a0;\nf.setCoeffs(aNorm, bNorm);","handlingStrategy":"validation","validationCode":"if (aCoeffs[0] == 0.0) throw new IllegalStateException(\"a0 must be non-zero; normalize denominator\");\nfilter.setCoeffs(aCoeffs, bCoeffs);","typeGuard":"public static boolean isNormalizedDenominator(double[] a) {\n    return a != null && a.length > 0 && a[0] == 1.0;\n}","tryCatchPattern":"try {\n    filter.setCoeffs(a, b);\n} catch (IllegalArgumentException e) {\n    double a0 = a[0];\n    double[] aN = Arrays.stream(a).map(v -> v / a0).toArray();\n    double[] bN = Arrays.stream(b).map(v -> v / a0).toArray();\n    filter.setCoeffs(aN, bN);\n}","preventionTips":["Always normalize the denominator so that a0 = 1 (standard IIR form).","Validate a[0] != 0 before calling.","Check design-tool output orientation if a0 is unexpectedly 0."],"tags":["audio","filter","argument-validation","division-by-zero","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}