Javascript IRR (Internal rate of return) Formula Accuracy

后端 未结 7 1592
名媛妹妹
名媛妹妹 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条回答
  •  感情败类
    2021-02-04 20:55

    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

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

    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;
    

    seems to me, to be the problem.

    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

提交回复
热议问题