英文:
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 <iostream>
#include <cmath>
using namespace std;
int main()
{
double futureValue, periodic_payment, rate_per_period;
int n; // number of payments
cout << "Enter the periodic payment, rate per period, and time of investment: ";
cin >> periodic_payment >> rate_per_period >> 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++ 代码。
正确的公式是:
英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论