TheAlgorithms/Java · error · IllegalArgumentException
Alpha must be between 0 and 1.
Error message
Alpha must be between 0 and 1.
What it means
Thrown by the EMAFilter constructor when the smoothing factor alpha is outside the valid half-open interval (0, 1]. An exponential moving average requires a positive weight on the current sample (alpha > 0), and a weight above 1 would amplify rather than smooth the signal. The boundary alpha = 0 (no update) and alpha > 1 (divergent filter) are both meaningless for an EMA and are rejected.
Source
Thrown at src/main/java/com/thealgorithms/audiofilters/EMAFilter.java:27
* The smoothing factor (alpha) controls the degree of smoothing.
*
* <p>
* Based on the definition from
* <a href="https://en.wikipedia.org/wiki/Moving_average">Wikipedia link</a>.
*/
public class EMAFilter {
private final double alpha;
private double emaValue;
/**
* Constructs an EMA filter with a given smoothing factor.
*
* @param alpha Smoothing factor (0 < alpha <= 1)
* @throws IllegalArgumentException if alpha is not in (0, 1]
*/
public EMAFilter(double alpha) {
if (alpha <= 0 || alpha > 1) {
throw new IllegalArgumentException("Alpha must be between 0 and 1.");
}
this.alpha = alpha;
this.emaValue = 0.0;
}
/**
* Applies the EMA filter to an audio signal array.
* EMA formula:
* EMA = alpha * currentSample + (1 - alpha) * previousEMA
*
* @param audioSignal Array of audio samples to process
* @return Array of processed (smoothed) samples
*/
public double[] apply(double[] audioSignal) {
if (audioSignal == null || audioSignal.length == 0) {
return new double[0];
}
double[] emaSignal = new double[audioSignal.length];View on GitHub (pinned to fdfb9a395b)
Solutions
- Ensure alpha is a fraction strictly greater than 0 and at most 1: clamp or validate before constructing, e.g. require 0 < alpha <= 1.
- If alpha comes from a period N, derive it as alpha = 2.0 / (N + 1.0) and reject N < 1 upstream.
- If the value originates from user/CLI input, parse and bounds-check it before passing it to the constructor.
Example fix
// before
new EMAFilter(0); // throws
// after
if (alpha <= 0 || alpha > 1) throw new IllegalArgumentException("alpha must be in (0,1]");
new EMAFilter(alpha); Defensive patterns
Strategy: validation
Validate before calling
public static boolean isValidAlpha(double alpha) {
return alpha > 0.0 && alpha <= 1.0;
}
// usage
if (!isValidAlpha(alpha)) throw new IllegalArgumentException("alpha must be in (0,1]");
EMAFilter filter = new EMAFilter(alpha); Type guard
public static boolean isValidAlpha(double alpha) {
return Double.isFinite(alpha) && alpha > 0.0 && alpha <= 1.0;
} Try / catch
try {
filter = new EMAFilter(alpha);
} catch (IllegalArgumentException e) {
// fall back to a safe default smoothing factor
filter = new EMAFilter(0.2);
} Prevention
- Treat alpha as a fraction (0..1), never a percentage.
- Derive alpha from a period N via alpha = 2/(N+1) and validate N >= 1.
- Validate alpha at the config/CLI boundary before it reaches the constructor.
When it happens
Trigger: Constructing `new EMAFilter(0.0)`, `new EMAFilter(-0.5)`, or `new EMAFilter(1.5)`. The check is `alpha <= 0 || alpha > 1`, so exactly 0 and anything strictly greater than 1 throw, while exactly 1.0 is accepted (pass-through).
Common situations: Reading alpha from a config file or CLI argument that defaults to 0 or is unset; computing alpha as `2/(N+1)` where N is derived from user input that can be 0 or negative; passing a percentage (e.g. 25 for 25%) instead of the fraction 0.25.
Related errors
- order must be greater than zero
- aCoeffs must be of size {}, got {}
- aCoeffs.get(0) must not be zero
- bCoeffs must be of size {}, got {}
- Invalid input: 0 ≤ k ≤ n is required.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/0c8ba101fee67f7c.
Report an issue: GitHub.