简体   繁体   English

为什么我从此XIRR函数获得NaN?

[英]Why am I getting NaN from this XIRR function?

This one is driving me up the wall. 这使我无法自拔。

UPDATE: Assembled in a jsfiddle that yields NaN: https://jsfiddle.net/eqcww2y7/6/ 更新:组装在产生NaN的jsfiddle中: https ://jsfiddle.net/eqcww2y7/6/

I'm using this XIRR function: https://gist.github.com/ghalimi/4669712 我正在使用此XIRR函数: https ://gist.github.com/ghalimi/4669712

And into that function, I'm sending a simple pair of arrays - 4 dates, 4 cashflow values. 在该函数中,我将发送一对简单的数组-4个日期,4个现金流量值。

var dates = [
  "2016-01-01",
  "2016-02-01",
  "2016-03-01",
  "2016-04-01",
];

var values = [
  -1000000,
  0,
  0,
  750000
];

console.log('dates', dates);
console.log('values', values);
console.log(XIRR(values, dates, 0.1));

The function has a dependency on momentjs, which is not causing any issues - console.log() inside the function shows the dates are being parsed correctly. 该函数依赖momentjs,不会引起任何问题-函数内部的console.log()显示正确地解析了日期。

That snippet above returns NaN, when it should return -0.68. 上面的代码段返回NaN,而应返回-0.68。 If I run those exact same values through Excel: 如果我通过Excel运行这些完全相同的值:

在Excel中测试XIRR

Can anyone spot the issue? 谁能发现这个问题? Is it with the function, am I sending the wrong values in? 函数是否可以发送错误的值?

@Andreas got to the heart of it. @Andreas着迷于此。 This XIRR function was written in 2012, and as of ECMAScript 2015, Math.pow() behaves differently. XIRR函数于2012年编写,从ECMAScript 2015开始,Math.pow()的行为有所不同。 I'm not sure if it's an issue that applies to all browsers/engines/implementations, but for my specific use case, XIRR() is returning NaN because the base and exponent inputs are going negative. 我不确定这是否适用于所有浏览器/引擎/实现,但是对于我的特定用例,由于基数和指数输入为负,因此XIRR()返回NaN。

UPDATE: To clarify - in my use case, the base/exponent pairs start out like this: 更新:为了澄清-在我的用例中,基数对/指数对开始是这样的:

x = 1.1, y = 0.0849
x = 1.1, y = 1.2493

But one iteration later I have this: 但是一个迭代之后,我有了这个:

x = -0.512, y = 0.084
x = -0.512, y = 1.164 

Since the x is a negative non-integer, I'm always gonna get NaN, no matter what the exponent is. 由于x是负的非整数,所以不管指数是多少,我总会得到NaN。 At least that's my understanding of the limitations of Math.pow(). 至少那是我对Math.pow()局限性的理解。

Just to add to the already given explanations, the XIRR function used in that gist mentions that 只是为了增加已经给出的解释,该要点中使用的XIRR函数提到:

Some algorithms have been ported from Apache OpenOffice 一些算法已从Apache OpenOffice移植

If you set up the same values/dates in OpenOffice Calc you get 如果您在OpenOffice Calc中设置相同的值/日期,则会得到

在此处输入图片说明


Searching for this error in relation to XIRR in the openoffice forums you get to https://forum.openoffice.org/en/forum/viewtopic.php?t=14006 where they mention that the likely problem is what we have identified here. 在openoffice论坛中搜索与XIRR相关的错误,您可以访问https://forum.openoffice.org/en/forum/viewtopic.php?t=14006 ,他们提到可能的问题是我们在此处找到的。

Solution

They do offer a solution though ( turns out it was a solution to specific constraints ), 他们确实提供了解决方案( 事实证明这是针对特定约束的解决方案 ),

I suggest that you use SUM(C4:C15)/C3 ( sum of the deposited values minus the received value ) as a guess 我建议您使用SUM(C4:C15)/ C3( 存入值的总和减去接收值 )作为猜测

In your case that would be SUM(B2:B4)/B1 and it indeed returns -0.684592 在您的情况下,它将是SUM(B2:B4)/B1 ,并且确实返回-0.684592

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM