TheAlgorithms/Java · error · IllegalArgumentException

aCoeffs.get(0) must not be zero

Error message

aCoeffs.get(0) must not be zero

What it means

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.

Source

Thrown at src/main/java/com/thealgorithms/audiofilters/IIRFilter.java:54

        historyX = new double[order];
        historyY = new double[order];
    }

    /**
     * Set coefficients
     *
     * @param aCoeffs Denominator coefficients
     * @param bCoeffs Numerator coefficients
     * @throws IllegalArgumentException if {@code aCoeffs} or {@code bCoeffs} is
     * not of size {@code order}, or if {@code aCoeffs[0]} is 0.0
     */
    public void setCoeffs(double[] aCoeffs, double[] bCoeffs) throws IllegalArgumentException {
        if (aCoeffs.length != order) {
            throw new IllegalArgumentException("aCoeffs must be of size " + order + ", got " + aCoeffs.length);
        }

        if (aCoeffs[0] == 0.0) {
            throw new IllegalArgumentException("aCoeffs.get(0) must not be zero");
        }

        if (bCoeffs.length != order) {
            throw new IllegalArgumentException("bCoeffs must be of size " + order + ", got " + bCoeffs.length);
        }

        for (int i = 0; i < order; i++) {
            coeffsA[i] = aCoeffs[i];
            coeffsB[i] = bCoeffs[i];
        }
    }

    /**
     * Process a single sample
     *
     * @param sample the sample to process
     * @return the processed sample
     */

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Normalize the denominator array by dividing every element by a[0] so that a[0] becomes 1.0 (standard IIR form).
  2. Verify the leading coefficient is non-zero before calling setCoeffs.
  3. Re-check the coefficient source if a0 unexpectedly arrives as 0.

Example fix

// before
f.setCoeffs(new double[]{0.0, 0.5, 0.25}, b);  // throws

// after
double a0 = a[0];
double[] aNorm = new double[a.length];
for (int i = 0; i < a.length; i++) aNorm[i] = a[i] / a0;
f.setCoeffs(aNorm, bNorm);
Defensive patterns

Strategy: validation

Validate before calling

if (aCoeffs[0] == 0.0) throw new IllegalStateException("a0 must be non-zero; normalize denominator");
filter.setCoeffs(aCoeffs, bCoeffs);

Type guard

public static boolean isNormalizedDenominator(double[] a) {
    return a != null && a.length > 0 && a[0] == 1.0;
}

Try / catch

try {
    filter.setCoeffs(a, b);
} catch (IllegalArgumentException e) {
    double a0 = a[0];
    double[] aN = Arrays.stream(a).map(v -> v / a0).toArray();
    double[] bN = Arrays.stream(b).map(v -> v / a0).toArray();
    filter.setCoeffs(aN, bN);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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