{"record":{"id":"8d399ef73ed89038","repo":"TheAlgorithms/C-Sharp","slug":"double-factorial-is-only-defined-for-non-negative-integers","errorCode":null,"errorMessage":"Double factorial is only defined for non-negative integers (num >= 0).","messagePattern":"Double factorial is only defined for non-negative integers \\(num >= 0\\)\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Numeric/DoubleFactorial.cs","lineNumber":22,"sourceCode":"\n/// <summary>\n///     The double factorial of a positive integer n, denoted by n!!,\n///     is the product of all integers from 1 up to n that have the same parity (odd or even) as n.\n///     E.g., 5!! = 5 * 3 * 1 = 15, and 6!! = 6 * 4 * 2 = 48.\n/// </summary>\npublic static class DoubleFactorial\n{\n    /// <summary>\n    ///     Calculates the double factorial of a non-negative integer number.\n    /// </summary>\n    /// <param name=\"inputNum\">Non-negative integer input number.</param>\n    /// <returns>Double factorial of the integer input number.</returns>\n    public static BigInteger Calculate(int inputNum)\n    {\n        // Don't calculate double factorial if input is a negative number.\n        if (inputNum < 0)\n        {\n            throw new ArgumentException(\"Double factorial is only defined for non-negative integers (num >= 0).\");\n        }\n\n        // Base cases: 0!! = 1 and 1!! = 1\n        if (inputNum <= 1)\n        {\n            return BigInteger.One;\n        }\n\n        // Initialize result.\n        BigInteger result = BigInteger.One;\n\n        // Start the iteration from the input number and step down by 2.\n        // This handles both odd (n, n-2, ..., 3, 1) and even (n, n-2, ..., 4, 2) cases naturally.\n        BigInteger current = inputNum;\n\n        while (current > BigInteger.Zero)\n        {\n            result *= current;","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Numeric/DoubleFactorial.cs#L4-L40","documentation":"EulerMethod.EulerFull integrates an ODE with the explicit Euler method from xStart to xEnd. It throws ArgumentOutOfRangeException when xStart >= xEnd because there would be no forward interval to integrate over. The message uses nameof interpolation and reads '{xEnd} should be greater than {xStart}'.","triggerScenarios":"Calling EulerFull with xEnd <= xStart, e.g. EulerFull(1.0, 1.0, ...) (equal bounds) or reversed bounds EulerFull(2.0, 0.0, ...); computing xEnd from a formula that lands before or on xStart.","commonSituations":"Backward integration attempted by swapping bounds (the method only integrates forward); unit tests with degenerate equal endpoints; time range configured from user input where start and end were entered in the wrong order.","solutions":["Ensure xEnd > xStart before calling; normalize with Math.Min/Math.Max if the input order is not guaranteed.","For backward integration, negate the derivative function and integrate forward from the later bound.","Validate the configured time range at the source (config parsing, UI input) rather than at the numeric call."],"exampleFix":"// before\nvar points = EulerMethod.EulerFull(tEnd, tStart, h, y0, f); // tEnd <= tStart throws\n// after\nif (tEnd <= tStart) throw new ArgumentException(\"tEnd must be after tStart\");\nvar points = EulerMethod.EulerFull(tStart, tEnd, h, y0, f);","handlingStrategy":"validation","validationCode":"if (xEnd <= xStart)\n    throw new ArgumentException(\"xEnd must be greater than xStart\");\nvar points = EulerMethod.EulerFull(xStart, xEnd, stepSize, yStart, yDerivative);","typeGuard":null,"tryCatchPattern":"try\n{\n    var points = EulerMethod.EulerFull(xStart, xEnd, h, y0, f);\n}\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"xEnd\")\n{\n    // handle reversed/degenerate interval\n}","preventionTips":["Normalize the interval with Math.Min/Math.Max before integrating.","For backward integration, flip the sign of the derivative instead of swapping bounds.","Validate time ranges where they are configured, not at the solver."],"tags":["argument-out-of-range","numerical-methods","ode","csharp"],"backgroundTag":"argument-out-of-range","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}