{"record":{"id":"feda6727eeeb343d","repo":"TheAlgorithms/C-Sharp","slug":"discount-rate-cannot-be-negative","errorCode":null,"errorMessage":"Discount rate cannot be negative","messagePattern":"Discount rate cannot be negative","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Financial/PresentValue.cs","lineNumber":12,"sourceCode":"namespace Algorithms.Financial;\n\n/// <summary>\n/// PresentValue is the value of an expected income stream determined as of the date of valuation.\n/// </summary>\npublic static class PresentValue\n{\n    public static double Calculate(double discountRate, List<double> cashFlows)\n    {\n        if (discountRate < 0)\n        {\n            throw new ArgumentException(\"Discount rate cannot be negative\");\n        }\n\n        if (cashFlows.Count == 0)\n        {\n            throw new ArgumentException(\"Cash flows list cannot be empty\");\n        }\n\n        double presentValue = cashFlows.Select((t, i) => t / Math.Pow(1 + discountRate, i)).Sum();\n\n        return Math.Round(presentValue, 2);\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":25,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Financial/PresentValue.cs#L1-L25","documentation":"PresentValue.Calculate computes the present value of a cash flow series discounted at the given rate. A negative discount rate is rejected with ArgumentException \"Discount rate cannot be negative\" because the discounting formula is not meaningful for negative rates in this implementation.","triggerScenarios":"Calling PresentValue.Calculate with discountRate < 0, e.g. Calculate(-0.05, new List<double> { 100, 200 }) — throws ArgumentException. Also happens when the rate is computed from other data (spread, difference of rates) and turns out negative.","commonSituations":"Passing a percentage (e.g. -5) instead of a signed decimal rate; computing the rate dynamically from market data that went negative; unit signs confusion when a negative rate was intended as 'add 5%' but supplied as -0.05.","solutions":["Pass a non-negative discount rate (use 0 for no discounting).","Clamp the rate before the call: Math.Max(0, discountRate).","If a negative rate is legitimate for your analysis, implement the formula yourself instead of using this helper.","Check the rate's units — convert a percentage like 5 to 0.05, and verify sign conventions in upstream calculations."],"exampleFix":"// before\nvar pv = PresentValue.Calculate(discountRate, cashFlows); // discountRate = -0.03\n// after\nvar rate = Math.Max(0, discountRate);\nvar pv = PresentValue.Calculate(rate, cashFlows);","handlingStrategy":"validation","validationCode":"if (discountRate < 0)\n    throw new ArgumentOutOfRangeException(nameof(discountRate), \"Discount rate must be non-negative.\");\nif (cashFlows == null || cashFlows.Count == 0)\n    throw new ArgumentException(\"Cash flows list cannot be empty.\");","typeGuard":"static bool IsValidPresentValueInput(double discountRate, List<double> cashFlows) =>\n    discountRate >= 0 && cashFlows != null && cashFlows.Count > 0;","tryCatchPattern":"try\n{\n    var pv = PresentValue.Calculate(discountRate, cashFlows);\n}\ncatch (ArgumentException ex)\n{\n    logger.LogWarning(ex, \"Invalid present-value input (rate={Rate})\", discountRate);\n    throw new InvalidOperationException(\"Provide a non-negative discount rate and at least one cash flow.\", ex);\n}","preventionTips":["Validate rate sign and cash-flow list at the financial-calculation boundary.","Clamp rates with Math.Max(0, rate) when dynamic inputs may go negative.","Standardize rate units (decimal fraction vs percent) across the codebase.","Cover negative-rate and empty-list cases with unit tests."],"tags":["csharp","finance","input-validation","argument-check"],"backgroundTag":"invalid-argument-value","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"}