{"record":{"id":"0c8ba101fee67f7c","repo":"TheAlgorithms/Java","slug":"alpha-must-be-between-0-and-1","errorCode":null,"errorMessage":"Alpha must be between 0 and 1.","messagePattern":"Alpha must be between 0 and 1\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/audiofilters/EMAFilter.java","lineNumber":27,"sourceCode":" * The smoothing factor (alpha) controls the degree of smoothing.\n *\n * <p>\n * Based on the definition from\n * <a href=\"https://en.wikipedia.org/wiki/Moving_average\">Wikipedia link</a>.\n */\npublic class EMAFilter {\n    private final double alpha;\n    private double emaValue;\n\n    /**\n     * Constructs an EMA filter with a given smoothing factor.\n     *\n     * @param alpha Smoothing factor (0 < alpha <= 1)\n     * @throws IllegalArgumentException if alpha is not in (0, 1]\n     */\n    public EMAFilter(double alpha) {\n        if (alpha <= 0 || alpha > 1) {\n            throw new IllegalArgumentException(\"Alpha must be between 0 and 1.\");\n        }\n        this.alpha = alpha;\n        this.emaValue = 0.0;\n    }\n\n    /**\n     * Applies the EMA filter to an audio signal array.\n     * EMA formula:\n     * EMA = alpha * currentSample + (1 - alpha) * previousEMA\n     *\n     * @param audioSignal Array of audio samples to process\n     * @return Array of processed (smoothed) samples\n     */\n    public double[] apply(double[] audioSignal) {\n        if (audioSignal == null || audioSignal.length == 0) {\n            return new double[0];\n        }\n        double[] emaSignal = new double[audioSignal.length];","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/audiofilters/EMAFilter.java#L9-L45","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\nnew EMAFilter(0);   // throws\n\n// after\nif (alpha <= 0 || alpha > 1) throw new IllegalArgumentException(\"alpha must be in (0,1]\");\nnew EMAFilter(alpha);","handlingStrategy":"validation","validationCode":"public static boolean isValidAlpha(double alpha) {\n    return alpha > 0.0 && alpha <= 1.0;\n}\n// usage\nif (!isValidAlpha(alpha)) throw new IllegalArgumentException(\"alpha must be in (0,1]\");\nEMAFilter filter = new EMAFilter(alpha);","typeGuard":"public static boolean isValidAlpha(double alpha) {\n    return Double.isFinite(alpha) && alpha > 0.0 && alpha <= 1.0;\n}","tryCatchPattern":"try {\n    filter = new EMAFilter(alpha);\n} catch (IllegalArgumentException e) {\n    // fall back to a safe default smoothing factor\n    filter = new EMAFilter(0.2);\n}","preventionTips":["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."],"tags":["audio","filter","constructor","argument-validation","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}