I had a coworker that wrote an IRR calculation function. You supply what seems to be set of cash flows, and it returns the IRR. It works 99% of the time but sometimes I get a floating point error. I am trying to figure out where in the code that can happen. The function looks like this:
CREATE FUNCTION [dbo].[CalcXIRR]
(
@Sample XIRRTable READONLY,
@Rate FLOAT = 0.1
)
RETURNS DECIMAL(38, 9)
AS
BEGIN
DECLARE @X FLOAT = 0.0,
@X0 FLOAT = 0.1,
@f FLOAT = 0.0,
@fbar FLOAT = 0.0,
@i TINYINT = 0,
@found TINYINT = 0
IF @Rate IS NULL
SET @Rate = 0.1
SET @X0 = @Rate
WHILE @i < 100
BEGIN
SELECT @f = 0.0,
@fbar = 0.0
SELECT @f = @f + value * POWER(1 + @X0, (-theDelta / 365.0E)),
@fbar = @fbar - theDelta / 365.0E * value * POWER(1 + @X0, (-theDelta / 365.0E - 1))
FROM
(
SELECT Value,
DATEDIFF(DAY, MIN(date) OVER (), date) AS theDelta
FROM @Sample
) AS d
SET @X = @X0 - @f / @fbar
IF ABS(@X - @X0) < 0.00000001
BEGIN
SET @found = 1
BREAK;
END
SET @X0 = @X
SET @i += 1
END
IF @found = 1
RETURN @X
RETURN NULL
END
GO
You supply it data that looks like this:
Date Value
2017-08-01 00:00:00.000 -3775585
2017-08-01 00:00:00.000 -10763.73
2017-08-01 00:00:00.000 -10763.73
2017-08-01 00:00:00.000 10763.73
2017-08-01 00:00:00.000 10763.73
2017-08-01 00:00:00.000 3775585
2017-08-01 00:00:00.000 3786348.73
2017-08-02 00:00:00.000 -1550780
2017-08-02 00:00:00.000 -4823
2017-08-02 00:00:00.000 -4823
2017-09-11 00:00:00.000 -254800
2017-09-11 00:00:00.000 254800