{"record":{"id":"cb4bf97fb1726e16","repo":"Humanizr/Humanizer","slug":"specified-argument-was-out-of-the-range-of-valid-v-cb4bf9","errorCode":null,"errorMessage":"Specified argument was out of the range of valid values. (Parameter 'tolerance')","messagePattern":"Specified argument was out of the range of valid values\\. \\(Parameter 'tolerance'\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Humanizer/FractionalizeExtensions.cs","lineNumber":49,"sourceCode":"    /// 1.25m.Fractionalize(5, 0m) => \"1 1/4\"\n    /// 0.34m.Fractionalize(5, 0.01m) => \"1/3\"\n    /// 0.75m.Fractionalize(4, 0m, useUnicode: true) => \"¾\"\n    /// </code>\n    /// </example>\n    public static string Fractionalize(\n        this decimal input,\n        int maxDenominator,\n        decimal tolerance,\n        bool useUnicode = false)\n    {\n#if NET8_0_OR_GREATER\n        ArgumentOutOfRangeException.ThrowIfLessThan(maxDenominator, 1);\n        ArgumentOutOfRangeException.ThrowIfNegative(tolerance);\n#else\n        if (maxDenominator < 1)\n            throw new ArgumentOutOfRangeException(nameof(maxDenominator));\n        if (tolerance < 0)\n            throw new ArgumentOutOfRangeException(nameof(tolerance));\n#endif\n\n        if (decimal.Truncate(input) == input)\n            return input.ToString(\"0\", CultureInfo.InvariantCulture);\n\n        var value = ToFraction(input);\n        var approximation = LimitDenominator(BigInteger.Abs(value.Numerator), value.Denominator, maxDenominator);\n        if (value.Numerator.Sign < 0)\n            approximation.Numerator = -approximation.Numerator;\n\n        if (!IsWithinTolerance(value, approximation, ToFraction(tolerance)))\n            return input.ToString(LocaleNumberFormattingOverrides.GetFormattingNumberFormat(CultureInfo.CurrentCulture));\n\n        return Format(approximation, useUnicode);\n    }\n\n    static (BigInteger Numerator, BigInteger Denominator) ToFraction(decimal value)\n    {","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/Humanizr/Humanizer/blob/ffc2b77c0f30d2fb176875841424379319d0ae9b/src/Humanizer/FractionalizeExtensions.cs#L31-L67","documentation":"Thrown by Fractionalize when tolerance is negative. Tolerance represents the maximum absolute difference allowed between the input and its fractional approximation, so a negative value is meaningless; zero is allowed (exact match required). On .NET 8+ it uses ArgumentOutOfRangeException.ThrowIfNegative; otherwise a manual throw. The parameter is named 'tolerance'.","triggerScenarios":"Calling value.Fractionalize(64, -0.01m); passing a tolerance computed as a difference that can go negative; deserializing tolerance from config where a sign error or missing value yields a negative decimal.","commonSituations":"Tolerance derived from a percentage/epsilon that underflows to a small negative due to floating/decimal arithmetic; configuration stored as a magnitude with a sign flag that is misread; reusing an 'allowed error' field for another purpose.","solutions":["Use Math.Abs(tolerance) or default to 0m when the computed tolerance is negative.","Validate tolerance at the configuration boundary and reject negative values with a descriptive error.","When you want an exact fraction, pass 0m explicitly rather than relying on a computed near-zero value."],"exampleFix":"// before\nvar f = value.Fractionalize(64, tolerance);\n\n// after\nvar tol = tolerance < 0 ? 0m : tolerance;\nvar f = value.Fractionalize(64, tol);","handlingStrategy":"validation","validationCode":"if (tolerance < 0)\n    throw new ArgumentOutOfRangeException(nameof(tolerance), \"Must be >= 0.\");\nvar f = input.Fractionalize(maxDenominator, tolerance);","typeGuard":"static bool IsValidTolerance(decimal t) => t >= 0;","tryCatchPattern":"try { return input.Fractionalize(maxDenominator, tolerance); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"tolerance\")\n{ return input.Fractionalize(maxDenominator, 0m); }","preventionTips":["Use Math.Abs or default to 0m when tolerance could be negative.","Validate tolerance magnitude at the configuration source.","Pass 0m explicitly for exact-fraction requirements."],"tags":["argument-validation","fractional","numeric","argument-out-of-range"],"backgroundTag":null,"analyzedSha":"ffc2b77c0f30d2fb176875841424379319d0ae9b","analyzedAt":"2026-08-13T21:42:34.584Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}