混淆了如何使用pow()函数来消极的东西?

时间:2014-03-12 19:37:45

标签: c++ visual-studio-2010

我不确定我是否在正确的位置使用了pow功能,但我想知道如何将月份设置为 - 月份供电。

以下是我假设的基本原则: 校长*(费率/ 12)/(1 - (1 +费率/ 12)^ - 月)

我收到错误消息: 错误C2064:术语不评估为采用1个参数的函数

这是我的代码:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int y = -1;
    double months;
    double principle = 1000;
    double rate = 7.20;
    double monthly_pay;

    cout << "Please enter the number of months you have to pay back the loan:";
    cin >> months;
    monthly_pay = principle*(rate/12)/((1-pow((1+rate/12), -months)));
    cout << monthly_pay << endl;

    return 0;
}

2 个答案:

答案 0 :(得分:1)

double pow (double base, double exponent);
      //Returns the value of the first argument raised to the power of the second argument.

所以改变

monthly_pay = principle*(rate/12)/(1-(1+rate/12)pow(months, y);

monthly_pay = principle*(rate/12)/((1-pow((1+rate/12), -months));

答案 1 :(得分:0)

我将你的算法分解成更小的函数(我希望你不介意)。我想出了这个编译得很好的代码。我相信你理解了这个概念,你只是忘了使用星号乘以pow(a,b)函数。

#include <iostream>
#include <cmath>

using namespace std;

int main()
{

int y = -1;
double months;
double principle = 1000;
double rate = 7.20;
double monthly_pay;
double a, b;


cout << "Please enter the number of months you have to pay back the loan:";

cin >> months;

a = rate / 12;
b = pow(months, y);

monthly_pay = (principle * a) / (1 - (1 + a) * b);


cout << monthly_pay << endl;


return 0;

}

我希望这有帮助!