{"record":{"id":"10a5189e2f399bd3","repo":"stride3d/stride","slug":"m-parameter-should-be-between-l-and-l","errorCode":null,"errorMessage":"'m' parameter should be between '-l' and '+l'.","messagePattern":"'m' parameter should be between '-l' and '\\+l'\\.","errorType":"exception","errorClass":"IndexOutOfRangeException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Mathematics/SphericalHarmonics.cs","lineNumber":101,"sourceCode":"            CheckIndicesValidity(l, m, order);\n            return Coefficients[LmToCoefficientIndex(l, m)];\n        }\n        set\n        {\n            CheckIndicesValidity(l, m, order);\n            Coefficients[LmToCoefficientIndex(l, m)] = value;\n        }\n    }\n\n    // ReSharper disable UnusedParameter.Local\n    private static void CheckIndicesValidity(int l, int m, int maxOrder)\n    // ReSharper restore UnusedParameter.Local\n    {\n        if (l > maxOrder - 1)\n            throw new IndexOutOfRangeException(\"'l' parameter should be between '0' and '{0}' (order-1).\".ToFormat(maxOrder - 1));\n\n        if (Math.Abs(m) > l)\n            throw new IndexOutOfRangeException(\"'m' parameter should be between '-l' and '+l'.\");\n    }\n\n    private static int LmToCoefficientIndex(int l, int m)\n    {\n        return l * l + l + m;\n    }\n}\n\n/// <summary>\n/// A spherical harmonics representation of a cubemap.\n/// </summary>\n[DataContract(\"SphericalHarmonics\")]\npublic class SphericalHarmonics : SphericalHarmonics<Color3>\n{\n    private readonly float[] baseValues;\n\n    private const float Pi4 = 4 * MathUtil.Pi;\n    private const float Pi16 = 16 * MathUtil.Pi;","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Mathematics/SphericalHarmonics.cs#L83-L119","documentation":"SphericalHarmonics.CheckIndicesValidity validates the (l, m) indices used to address a spherical harmonic basis function. For a given degree l, the order m must satisfy |m| <= l; otherwise the index pair does not identify a valid harmonic. The library throws IndexOutOfRangeException with this message when Math.Abs(m) > l.","triggerScenarios":"Calling SphericalHarmonics evaluation/constructor helpers (e.g. SphericalHarmonics.FromBasicTerms / Evaluate-style APIs) with an (l, m) pair where l is within maxOrder-1 but m is outside [-l, +l], such as l=1 with m=2 or l=0 with m=-1.","commonSituations":"Hand-rolling loops over harmonic coefficients with wrong bounds (iterating m over the full order range instead of -l..l); porting shader or physics code that uses a different (m, l) convention; hardcoding indices without the |m|<=l constraint.","solutions":["Clamp or validate m so that -l <= m <= l before calling the API.","Fix loop bounds: iterate m from -l to l (inclusive) per degree l, not over a fixed range.","Verify the l/m argument order was not swapped at the call site (a common convention mistake).","Use LmToCoefficientIndex(l, m) consistently so coefficient indexing matches the validated (l, m) pairs."],"exampleFix":"// before\nsh.SetCoefficient(1, 2, value); // l=1, m=2 -> |m| > l\n// after\nint l = 1, m = 1;\nif (Math.Abs(m) <= l)\n    sh.SetCoefficient(l, m, value);","handlingStrategy":"validation","validationCode":"int l = /* desired degree */, m = /* desired order */;\nint maxOrder = 2; // library maxOrder, adjust per usage\nif (l < 0 || l > maxOrder - 1)\n    throw new ArgumentOutOfRangeException(nameof(l), $\"'l' must be between 0 and {maxOrder - 1}.\");\nif (Math.Abs(m) > l)\n    throw new ArgumentOutOfRangeException(nameof(m), $\"'m' must be between -l and +l for l={l}.\");","typeGuard":null,"tryCatchPattern":"try\n{\n    sh.SetValue(l, m, value);\n}\ncatch (IndexOutOfRangeException ex)\n{\n    // invalid (l, m) pair — clamp m to [-l, l] and retry or log\n    int clamped = Math.Clamp(m, -l, l);\n    sh.SetValue(l, clamped, value);\n}","preventionTips":["Iterate m from -l to l for each degree l, never over a fixed range.","Double-check l/m argument order against the library convention.","Add an assertion |m| <= l in harmonic coefficient loops."],"tags":["mathematics","argument-out-of-range","spherical-harmonics"],"backgroundTag":"value-out-of-range","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}