TheAlgorithms/C-Sharp · error · ArgumentException

Error value is not on interval (0.0; 1.0).

Error message

Error value is not on interval (0.0; 1.0).

What it means

Maclaurin-series approximations of Exp, Sin, and Cos require the caller to supply a truncation error bound that is a probability-like fraction strictly between 0 and 1. The library throws this ArgumentException immediately when error <= 0 or error >= 1 because the number of series terms it computes is derived from that bound; a value outside the open interval cannot define a meaningful tolerance.

Solutions

  1. Pass a strict open-interval value such as 0.0001 or 1e-6 as the error argument
  2. Clamp and validate the value before calling: if (error <= 0 || error >= 1) adjust it
  3. If you intended 100% accuracy, use double.MaxValue iterations manually or a different API; the series needs a positive tolerance

Example fix

// before
var e = Algorithms.Numeric.Maclaurin.Exp(1.0, 1.0); // throws
// after
var e = Algorithms.Numeric.Maclaurin.Exp(1.0, 0.0001);
Defensive patterns

Strategy: validation

Validate before calling

if (double.IsNaN(error) || error <= 0.0 || error >= 1.0)
    throw new ArgumentException("error must be strictly between 0 and 1", nameof(error));

Type guard

static bool IsValidError(double e) => !double.IsNaN(e) && e > 0.0 && e < 1.0;

Try / catch

try { var y = Maclaurin.Exp(x, error); }
catch (ArgumentException ex) when (ex.Message.Contains("interval")) { /* use a default tolerance like 1e-6 */ }

Prevention

When it happens

Trigger: Calling Exp, Sin, or Cos (which forward to ErrorTermWrapper) with error = 0, error = 1, a negative error, or a value like 1.0 computed from an off-by-one (e.g. 1 - 0.0).

Common situations: Passing a percentage (e.g. 1 meaning 100% precision) instead of a fraction; computing the error as a difference of doubles that rounded to exactly 0 or 1; copy-pasting epsilon values from other numeric libraries with different conventions.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/16d5f073afcbcade. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Numeric/Series/Maclaurin.cs:93

    /// <param name="error">Last term error value.</param>
    /// <returns>Approximated value of the function in the given point.</returns>
    /// <exception cref="ArgumentException">Error value is not on interval (0.0; 1.0).</exception>
    public static double Cos(double x, double error = 0.00001) => ErrorTermWrapper(x, error, CosTerm);

    /// <summary>
    ///     Wrapper function for calculating approximation with estimated
    ///     count of terms, where last term value is less than given error.
    /// </summary>
    /// <param name="x">Given point.</param>
    /// <param name="error">Last term error value.</param>
    /// <param name="term">Indexed term of approximation series.</param>
    /// <returns>Approximated value of the function in the given point.</returns>
    /// <exception cref="ArgumentException">Error value is not on interval (0.0; 1.0).</exception>
    private static double ErrorTermWrapper(double x, double error, Func<double, int, double> term)
    {
        if (error <= 0.0 || error >= 1.0)
        {
            throw new ArgumentException("Error value is not on interval (0.0; 1.0).");
        }

        var i = 0;
        var termCoefficient = 0.0;
        var result = 0.0;

        do
        {
            result += termCoefficient;
            termCoefficient = term(x, i);
            i++;
        }
        while (Math.Abs(termCoefficient) > error);

        return result;
    }

    /// <summary>

View on GitHub (pinned to 96e2905cab)