简体   繁体   English

iPhone SDK现值功能

[英]iPhone SDK Present Value function

Present value is the value on a given date of a future payment or series of future payments, discounted to reflect the time value of money and other factors such as investment risk. 现值是未来付款或一系列未来付款在给定日期的价值,折现后可以反映货币的时间价值和其他因素,例如投资风险。 Present value calculations are widely used in business and economics to provide a means to compare cash flows at different times on a meaningful "like to like" basis. 现值计算在商业和经济学中被广泛使用,以提供一种手段在有意义的“喜欢”基础上比较不同时间的现金流量。

http://en.wikipedia.org/wiki/Present_value http://en.wikipedia.org/wiki/Present_value

What would be the best way to tackle this in an Objective-C function? 在Objective-C功能中解决此问题的最佳方法是什么?

double retire62 = [benefit62.text doubleValue] * [yearlyReturn.text doubleValue] *12* [lblAgeExpectancy.text doubleValue];
double retire66 = [benefit66.text doubleValue] * [yearlyReturn.text doubleValue] *12* [lblAgeExpectancy.text doubleValue];
double retire70 = [benefit70.text doubleValue] * [yearlyReturn.text doubleValue] *12* [lblAgeExpectancy.text doubleValue];

Im just not familiar with Present Value/ 我只是不熟悉现值/

Putting aside your code snippet for a little while, the present value calculation itself is reasonably straightforward, a least in its simpler forms. 将您的代码段搁置一会儿,现值计算本身就相当简单,至少在形式上更简单。 (It can become a fair bit more complicated if you start to consider more realistic interest with different rates at different terms and such, but if you want to get into that you'll need to do some proper reading up.) (如果您开始考虑以不同的利率和不同的利率来考虑更切合实际的利率,这可能会变得稍微复杂一些,但是如果您想了解这一点,则需要进行一些适当的阅读。)

The present value of any single future cash flow is the amount of money you would have to invest now (at the so-called risk-free rate ) in order to have the future amount when the time comes. 任何单一的未来现金流量的现值是你必须现在投资(在所谓的金额无风险利率 ,才能有未来的量在时机成熟时)。 That is, it is the future amount discounted at the specified interest rate. 也就是说,它是按指定利率折现的未来金额。

As a trivial example, suppose you are going to give me $105 in a year's time, and the annual interest rate is 5%. 举个简单的例子,假设您一年后要给我$ 105,年利率是5%。 If I have $100 now and invest it at that rate, in a year's time I will have $105, the same amount you are due to give me. 如果我现在有100美元,并以该比率进行投资,那么一年后,我将有105美元,这是您应付给我的金额。 So the present value of that future $105 is not $105, but only $100. 因此,那个未来$ 105的不是 $ 105,而是只有$ 100。 (This is just a slightly more formal equivalent of observing that a bird in the hand is worth two in the bush.) (这只是观察到手里的一只鸟在丛林中值得两只鸟的形式,在形式上稍等一些。)

Let's take a marginally more realistic example just to see how the calculation works. 让我们以一个稍微现实的示例为例,看看计算如何工作。 Suppose I'm due to receive $1000 in 5 years -- how much is that worth to me now? 假设我要在5年后收到1000美元-现在对我来说有多少价值?

Assume again that the relevant interest rate is 5% per year, and further assume that it is compounded annually -- which is to say, after a year the first 5% is added to the original amount and this combined amount then accrues interest over the second year, and so on. 再次假设相关利率为每年5%,并进一步假定它是每年复利的-也就是说,一年后,将第一个5%添加到原始金额中,然后这笔合并后的金额会在第二年,依此类推。 After each year I have the amount I started with the beginning plus the 5% interest on the amount -- that is 1.05 times what I started with at the beginning of the year. 每年之后,我都有从头开始的金额加上该金额的5%的利息-这是我从年初开始的金额的1.05倍。 So after five years I would have 1.05 * 1.05 * 1.05 * 1.05 * 1.05 times as much as I had right at the beginning. 因此,五年后,我的1.05 * 1.05 * 1.05 * 1.05 * 1.05将是刚开始时的1.05 * 1.05 * 1.05 * 1.05 * 1.05倍。 To have $1000 in five years I would have to invest $ 1000 / 1.05 * 1.05 * 1.05 * 1.05 * 1.05 , or about $784 -- and that's the present value of that $1000. 要在五年内拥有1000美元,我将不得不投资1000 / 1.05 * 1.05 * 1.05 * 1.05 * 1.05美元1000 / 1.05 * 1.05 * 1.05 * 1.05 * 1.05 ,约合784美元-这就是那1000美元的现值。

More generally, you would need to divide the future amount by pow(1 + r, n) for interest rate r and number of years n (or equivalently multiply by pow(1 + r, -n) ), and there are simple generalizations for where the interest rate and payments are over different periods (eg, annual rate compounded monthly). 更一般地,你将需要通过划分未来量pow(1 + r, n)利率r和若干年n (通过或等效乘法pow(1 + r, -n)并有简单的概括利率和还款期不同的地方(例如,年利率按月复利)。 See, eg, Wikipedia's compound interest entry for more detail. 有关更多详细信息,请参见例如Wikipedia的复利条目

OK, back to the question. 好,回到问题。 Coding this calculation in Objective-C is no different from coding it in C. Again using the simple version described above: 用Objective-C编码此计算与用C编码无异。再次使用上述简单版本:

double presentValue ( double futureValue, double annualRate, unsigned int years )
{
    return futureValue * pow ( 1 + annualRate, -years );
}

You could do this as an Obj-C method rather than a C function, but the essence would be pretty similar. 您可以将其作为Obj-C方法而不是C函数来执行,但是本质上将非常相似。 Adding more sophistication in terms of compounding periods and such is left as an exercise. 在复利期方面增加更多的复杂性,这是一项练习。

Note, however, that this doesn't bear very much resemblance to your own code, which looks to be just grabbing values blindly from text fields and multiplying them together. 但是请注意,这与您自己的代码不太相似,后者看起来只是从文本字段中盲目获取值并将它们相乘。 If you find yourself doing maths directly on the contents of views, you probably ought to be hearing alarm bells somewhere. 如果您发现自己直接根据视图的内容进行数学运算,则您可能应该在某个地方听到警钟声。

Not wishing to sound too high-horsey, but it seems to me that you need much more clearly to distinguish the underlying data (we could call it the model if we were being fancy) from the inputs (in other words, what is the user actually providing and what is the program expected to know how to do without them having to think about it). 不想听起来太刺耳,但是在我看来,您需要更加清晰地从输入(换句话说,用户是什么)中区分基础数据(如果我们愿意,可以将其称为模型)。实际提供的内容以及该程序在无需他们考虑的情况下应该知道如何做)。 And furthermore, both these things should be considered separately from the UI elements used to represent them. 而且,这两种情况都应与用于表示它们的UI元素分开考虑。

In other words, you probably need to revisit the whole Model-View-Controller pattern that every iPhone developer is supposed to have tattooed on their heart ;) 换句话说,您可能需要重新查看每个iPhone开发人员都应该在自己的心脏上刺青的整个Model-View-Controller模式;)

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

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