iphone - 在 Objective-C 中以编程方式计算 IRR(内部 yield )和 NPV

标签 iphone ios objective-c c excel

我正在开发一个财务应用程序,需要IRR(Excel 的内置功能) 计算,并在C 中找到了如此出色的教程here以及 C# 中的此类答案 here .

我实现了上面的C语言代码,但是当IRR为正时,它给出了完美的结果。它没有在应该返回负值的时候返回负值。而在 Excel 中,=IRR(values,guessrate) 也会对某些值返回负 IRR。

我也引用了上面的C#链接中的代码,看起来它遵循良好的程序并返回错误,也希望它也返回负的IRR,与Excel一样。但是我不熟悉 C#,所以我无法在 Objective-C 或 C 中实现相同的代码。

我正在从上面的链接编写 C 代码,我已实现这些代码以帮助你们。

#define LOW_RATE 0.01
#define HIGH_RATE 0.5
#define MAX_ITERATION 1000
#define PRECISION_REQ 0.00000001
double computeIRR(double cf[], int numOfFlows)
{
    int i = 0, j = 0;
    double m = 0.0;
    double old = 0.00;
    double new = 0.00;
    double oldguessRate = LOW_RATE;
    double newguessRate = LOW_RATE;
    double guessRate = LOW_RATE;
    double lowGuessRate = LOW_RATE;
    double highGuessRate = HIGH_RATE;
    double npv = 0.0;
    double denom = 0.0;
    for (i=0; i<MAX_ITERATION; i++)
    {
        npv = 0.00;
        for (j=0; j<numOfFlows; j++)
        {
            denom = pow((1 + guessRate),j);
            npv = npv + (cf[j]/denom);
        }

        /* Stop checking once the required precision is achieved */
        if ((npv > 0) && (npv < PRECISION_REQ))
            break;
        if (old == 0)
            old = npv;
        else
            old = new;
        new = npv;
        if (i > 0)
        {
            if (old < new)
            {
                if (old < 0 && new < 0)
                    highGuessRate = newguessRate;
                else
                    lowGuessRate = newguessRate;
            }
            else
            {
                if (old > 0 && new > 0)
                    lowGuessRate = newguessRate;
                else
                    highGuessRate = newguessRate;
                }
        }
        oldguessRate = guessRate;
        guessRate = (lowGuessRate + highGuessRate) / 2;
        newguessRate = guessRate;
    }
    return guessRate;
}

我附上了一些在 Excel 和上面的 C 语言代码中不同的值的结果。

 Values:             Output of Excel: -33.5%
 1 = -18.5,          Output of C code: 0.010 or say (1.0%)
 2 =  -18.5,
 3 = -18.5,
 4 = -18.5,
 5 = -18.5,
 6 =  32.0

Guess rate: 0.1

最佳答案

由于 low_rate 和 high_rate 都是正数,您无法获得负分。你必须改变:

#define LOW_RATE 0.01

例如,

#define LOW_RATE -0.5

关于iphone - 在 Objective-C 中以编程方式计算 IRR(内部 yield )和 NPV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17759427/

相关文章:

iphone - UIScrollView内容偏移问题

ios - 尝试使用 AV Foundation 捕获静止图像时不断出现 "inactive/invalid connection passed"错误

objective-c - 在 connectionDidFinishLoading :, NSURLConnection 委托(delegate)方法中发布后无法收到通知

iphone - 如何在不打开应用程序的情况下更改应用程序图标?

ios - Swift(iOS) 将 UIImage 数组插入 subview ?

ios - 更改为团队 ID 前缀后处理钥匙串(keychain)丢失

iphone - 将 UIActivityIndi​​cator 添加到 UITableView

ios - 在双值比较 iOS 中得到奇怪的结果

ios - 使用 Storyboard时覆盖默认初始化程序(构造函数依赖注入(inject))

objective-c - NSTextView 中的圆角和渐变