admin管理员组文章数量:1289828
I found a sample function for calculating the equivalent of Excel IRR using sample test data but I keep getting this error:
Arithmetic overflow error converting numeric to data type numeric.
which occurs on line 65 e.g.
SET @rate = @rate - @npv / (@new_npv - @npv);
This is the query:
-- Create the cash_flows table
CREATE TABLE cash_flows (
id INT PRIMARY KEY,
period INT,
amount DECIMAL(18, 2)
);
-- Insert example data
INSERT INTO cash_flows (id, period, amount) VALUES
(1, 0, -10000), -- Initial investment (negative cash flow)
(2, 1, 2000), -- Cash flow at period 1
(3, 2, 3000), -- Cash flow at period 2
(4, 3, 4000), -- Cash flow at period 3
(5, 4, 5000); -- Cash flow at period 4
-- Function to calculate NPV for a given rate
DROP FUNCTION IF EXISTS NPV
GO
CREATE FUNCTION dbo.NPV (@rate DECIMAL(18, 10))
RETURNS DECIMAL(18, 10)
AS
BEGIN
DECLARE @npv DECIMAL(18, 10) = 0;
SELECT @npv = SUM((amount / POWER(1 + @rate, period)))
FROM cash_flows;
RETURN @npv;
END;
-- Define variables for the IRR calculation
DECLARE @guess DECIMAL(18, 10) = 0.1;
DECLARE @precision DECIMAL(18, 10) = 0.00001;
DECLARE @max_iterations INT = 1000;
DECLARE @iteration INT = 0;
DECLARE @npv DECIMAL(18, 10);
DECLARE @new_npv DECIMAL(18, 10);
DECLARE @rate DECIMAL(18, 10) = @guess;
-- Calculate IRR using iterative approach
WHILE @iteration < @max_iterations
BEGIN
SET @npv = dbo.NPV(@rate);
IF ABS(@npv) < @precision
BREAK;
SET @new_npv = dbo.NPV(@rate + @precision);
SET @rate = @rate - @npv / (@new_npv - @npv);
SET @iteration = @iteration + 1;
END;
This is supposed to create a sample table for cashflows for a loan and calculate the EIR based on the cashflows table. The loan is for 10000 and the expected cashflows per period are listed in the table.
Please assist to identify what needs to be changed or cast to remove the error. The expected output is an IRR rate value based on the computation. First of all, I'm not entirely certain the query is correct but it runs. I got it from Microsoft copilot.
I found a sample function for calculating the equivalent of Excel IRR using sample test data but I keep getting this error:
Arithmetic overflow error converting numeric to data type numeric.
which occurs on line 65 e.g.
SET @rate = @rate - @npv / (@new_npv - @npv);
This is the query:
-- Create the cash_flows table
CREATE TABLE cash_flows (
id INT PRIMARY KEY,
period INT,
amount DECIMAL(18, 2)
);
-- Insert example data
INSERT INTO cash_flows (id, period, amount) VALUES
(1, 0, -10000), -- Initial investment (negative cash flow)
(2, 1, 2000), -- Cash flow at period 1
(3, 2, 3000), -- Cash flow at period 2
(4, 3, 4000), -- Cash flow at period 3
(5, 4, 5000); -- Cash flow at period 4
-- Function to calculate NPV for a given rate
DROP FUNCTION IF EXISTS NPV
GO
CREATE FUNCTION dbo.NPV (@rate DECIMAL(18, 10))
RETURNS DECIMAL(18, 10)
AS
BEGIN
DECLARE @npv DECIMAL(18, 10) = 0;
SELECT @npv = SUM((amount / POWER(1 + @rate, period)))
FROM cash_flows;
RETURN @npv;
END;
-- Define variables for the IRR calculation
DECLARE @guess DECIMAL(18, 10) = 0.1;
DECLARE @precision DECIMAL(18, 10) = 0.00001;
DECLARE @max_iterations INT = 1000;
DECLARE @iteration INT = 0;
DECLARE @npv DECIMAL(18, 10);
DECLARE @new_npv DECIMAL(18, 10);
DECLARE @rate DECIMAL(18, 10) = @guess;
-- Calculate IRR using iterative approach
WHILE @iteration < @max_iterations
BEGIN
SET @npv = dbo.NPV(@rate);
IF ABS(@npv) < @precision
BREAK;
SET @new_npv = dbo.NPV(@rate + @precision);
SET @rate = @rate - @npv / (@new_npv - @npv);
SET @iteration = @iteration + 1;
END;
This is supposed to create a sample table for cashflows for a loan and calculate the EIR based on the cashflows table. The loan is for 10000 and the expected cashflows per period are listed in the table.
Please assist to identify what needs to be changed or cast to remove the error. The expected output is an IRR rate value based on the computation. First of all, I'm not entirely certain the query is correct but it runs. I got it from Microsoft copilot.
Share Improve this question edited Feb 19 at 21:11 Dale K 27.5k15 gold badges58 silver badges83 bronze badges asked Feb 19 at 19:32 Emmanuel SalauEmmanuel Salau 236 bronze badges 15 | Show 10 more comments1 Answer
Reset to default 5Your new rate calculation appears to be missing a term, causing the algorithm to go wildly unstable.
Try:
SET @rate = @rate - @npv * @precision / (@new_npv - @npv);
This will yield a calculated final rate = 0.1282572690.
See this db<>fiddle.
Explanation: Your calculation is a variation on Newtons Method to solve for f(x) = 0, where:
- xn+1 = xn -
本文标签: sqlNumeric overflow error when attempting to calculate EIR with a functionStack Overflow
版权声明:本文标题:sql - Numeric overflow error when attempting to calculate EIR with a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741474267a2380787.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
-3571162108885.27834640677142857142
to the variable@rate
; that won't fit.@rate
is defined as aDECIMAL(18, 10)
so it can only have 8 digits before the decimal point; the value you attempt to assign has 13. db<<fiddle with simple debugging added. – Thom A Commented Feb 19 at 19:56-3571162108885.27834640677142857142
to-62108885.27834640677
, @EmmanuelSalau ? No, that isn't how assignments work. – Thom A Commented Feb 19 at 21:15