如何在C++中计算复利?

huangapple go评论59阅读模式
英文:

How to Calculate Compound Interest in C++?

问题

这是您的C++代码的翻译部分,代码部分不需要翻译:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{				
    double futureValue, periodic_payment, rate_per_period;
    int n; // number of payments

    cout << "输入定期支付金额、每期利率和投资时间:";
    cin >> periodic_payment >> rate_per_period >> n;

    // 计算第35个生日时的未来价值
    futureValue = periodic_payment * pow((1 + rate_per_period, n) * 0.01) / (rate_per_period));

    return 0;
}

FV = PMT{(1+i)^n-1 / i} 这个公式应该用以下代码表示:

futureValue = periodic_payment * ((pow(1 + rate_per_period, n) - 1) / rate_per_period);

这个公式计算的是在35个投资期之后的未来价值。

英文:

I'm trying to write a program to calculate compound interest, but I'm not sure how to use the pow() function. My assignment is this:

> Dvijesh makes his first $1,025.75 deposit into an IRA earning 4.125% compounded annually on his 24th birthday and his last $1,025.75 deposit on his 35th birthday. With no additional deposits, the money in the IRA continues to earn 4.125% interest compounded monthly until Dvijesh retires on his 65th birthday. Write a program to find out the amount of money that Dvijesh has in his IRA on his 35th and 65th birthday? How much interest did Dvijesh earn on his 35th and 65th birthday?
>
> FV = PMT{(1+i)^n-1 / i}
>
> A=P(1+i)^n
>
> FV:Future Value; PMT:Periodic Payment; i:Rate Per Period; n:Number Of
Payments; A:Future Amount; P:Principal

Right now, I'm trying to calculate the first formula, and this is what I have so far:

#include &lt;iostream&gt;
#include &lt;cmath&gt;
using namespace std;

int main()
{				
    double futureValue, periodic_payment, rate_per_period;
    int n; // number of payments

    cout &lt;&lt; &quot;Enter the periodic payment, rate per period, and time of investment: &quot;;
    cin &gt;&gt; periodic_payment &gt;&gt; rate_per_period &gt;&gt; n;

    // Future value of 35th birthday
    futureValue = periodic_payment * pow((1 + rate_per_period, n) * 0.01) / (rate_per_period));

    return 0;
}

We wrote something similar in my C++ class, but the formula was different. I'm not sure how to write FV = PMT{(1+i)^n-1 / i} in C++.

答案1

得分: 2

pow()(以及std::pow())接受两个输入参数,但您只传递了一个值 - 这个计算的结果:

(1 + rate_per_period, n) * 0.01

由于您使用了逗号运算符(1 + rate_per_period, n)返回n,因此上面的计算实际上只是这个更简单的计算:

n * 0.01

这是您传递给pow()的唯一值。但这不是它需要的。

公式(1+i)^n-1 / i并不翻译成您编写的这段代码:

pow((1 + rate_per_period, n) * 0.01)

实际上,它更像是下面的代码:

pow(1 + rate_per_period, n - 1 / i)

或者:

pow(1 + rate_per_period, n - 1) / i

或者:

pow(1 + rate_per_period, n) - 1 / i

取决于您如何解读公式。但是,这些解决方案都是错误的,因为您展示的公式本身就是错误的!所以,您把它们错误地转换成了 C++ 代码。

正确的公式是:

如何在C++中计算复利?

英文:

pow() (and std::pow()) takes 2 input arguments, but you are passing in only 1 value - the result of this calculation:

(1 + rate_per_period, n) * 0.01

Because of your use of the comma operator, (1 + rate_per_period, n) returns n, thus the above is effectively just this simpler calculation:

n * 0.01

That is the sole value you are passing to pow(). But that is not what it wants.

The formula (1+i)^n-1 / i does not translate into this code, as you have written it:

pow((1 + rate_per_period, n) * 0.01)

It actually translates into code more like this instead:

pow(1 + rate_per_period, n - 1 / i)

or:

pow(1 + rate_per_period, n - 1) / i

or:

pow(1 + rate_per_period, n) - 1 / i

Depending on how you read the formula. However, those solutions are all wrong, because the formulas you have shown are written incorrectly to begin with! So, you are translating them into C++ code incorrectly.

The correct formula is:

如何在C++中计算复利?

huangapple
  • 本文由 发表于 2023年2月18日 09:57:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490717.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定