Javascript IRR (Internal rate of return) Formula Accuracy

后端 未结 7 1593
名媛妹妹
名媛妹妹 2021-02-04 20:00

I\'m using a IRR function in javascript to create calculation a that is done in excel using its own IRR function. The problem is mine is little off and I have no idea why. Here\

7条回答
  •  闹比i
    闹比i (楼主)
    2021-02-04 20:55

    This might be a better way of writing the calculation. This allows the code to calculate instances of negative IRR or IRR above 100%.

            var IRRval = [];
    
            IRRval.push(-financed);
                for (i = 0; i < period; i++) {
                IRRval.push(rental);
            }
            var IRR = IRRCalc(IRRval, 0.001) * 0.01;
    
            function IRRCalc(CArray) {
                var r = 0
                min = -1.0;
                max = 10000.0;
                do {
                    guest = (min + max) / 2;
                    NPV = 0;
                    for (var j = 0; j < CArray.length; j++) {
                        NPV += CArray[j] / Math.pow((1 + guest), j);
                    }
                    if (NPV > 0) {
                        min = guest;
                    }
                    else {
                        max = guest;
                    }
                    r++
                } while (r < 100);
                return guest * 100
            }
    

提交回复
热议问题