Javascript IRR(内部收益率)公式准确度 [英] Javascript IRR (Internal rate of return) Formula Accuracy

查看:963
本文介绍了Javascript IRR(内部收益率)公式准确度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript中使用IRR函数来创建使用自己的IRR函数在excel中完成的计算。问题是我的小事,我不知道为什么。以下是下面的代码。

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's the code below.

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, guest) {
    inc = 0.000001;
    do {
        guest += inc;
        NPV = 0;
        for (var j=0; j < CArray.length; j++) {
            NPV += CArray[j] / Math.pow((1 + guest), j);
        }
    } while (NPV > 0);
    return guest * 100;
}

现在,如果您使用这些数字:

Now if you use these figures:

期间24

融资22000

出租1017.5000

Rental 1017.5000

我的结果是:0.008523000000000175

My Result is: 0.008523000000000175

Excel结果是:0.008522918

Excel Result is: 0.008522918

OR

期间42

融资218000

出租5917.1429

Rental 5917.1429

我的结果是:0.006247000000000489

My Result is: 0.006247000000000489

Excel结果是:0.00624616

Excel Result is: 0.00624616

Excel函数被调用: = IRR(T12:T73,0.01) T12-T73与我正在使用的数字相同。

The Excel function is called: =IRR(T12:T73,0.01) T12-T73 is the same figures I'm using.

任何帮助将不胜感激,感谢Jason

Any help would be much appreciated, Thanks Jason

更新

我已经通过更改下面的值来解决它。但现在的表现太慢了。任何关于如何改善这一点的想法?

I've solved it by changing the values below. But now the performance is too slow. Any ideas on how to improve this?

IRRCalc(IRRval, 0.001)

//to

IRRCalc(IRRval, 0.0001)

inc = 0.000001;

//to

inc = 0.00000001;


推荐答案

快速浏览您的代码后,错误似乎与浮点精度错误有关。有关更多信息,请访问: http://ajaxian.com/archives/在javascript中的浮点数据

After a quick skim read through your code, the error seems to be related to floating point precision error. More information can be found here: http://ajaxian.com/archives/crock-on-floating-points-in-javascript

在较旧的JavaScript引擎中,如果您做了0.3 + 0.3,则会得到类似于0.600000000001

In older javascript engines, if you did 0.3 + 0.3 you get something like 0.600000000001

尽管目前大多数JavaScript引擎返回0.6,但在引擎盖下,问题依然存在。一起添加浮点将导致意外的结果。所以在你的情况下,

Though most javascript engines today return 0.6, under the hood, the problem remains. Adding floats together causes unexpected results. So in your case the

inc = 0.000001;
guest += inc;

在我看来,成为问题。

解决这个问题的方法是使用整数。所以,而不是0.000001你将使用1而不是0.001你将使用1000.然后将您的返回结果除以100000

A way to solve this would be to use whole numbers. So instead of 0.000001 you would use 1 and instead of 0.001 you would use 1000. Then divide your return result by 100000

这篇关于Javascript IRR(内部收益率)公式准确度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆