带变量的 C++ 幂函数

标签 c++ pow

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

int main(int argc, char *argv[])
{
    double amount, rate, time, interest, month;
    interest = rate/(12.0*100);
    month = (amount * rate)/1.0 -(1.0 + interest), pow(time*12);
    cout << "What is the amount of the loan? $ ";
    cin >> amount;
    cout << "What is the annual percentage rate? ";
    cin >> rate;
    cout << "How many years will you take to pay back the loan?  ";
    cin >> time;
    cout <<"\n-------------------------------------------------------\n";
    cout << "Amount        Annual % Interest       Years         Monthly Payment\n\n\n";
    cout <<amount <<"                      " <<rate <<"               " <<time << "        " <<month;
    cout <<"\n\n";
    system("PAUSE");
    return EXIT_SUCCESS;
}

我在这里遇到错误,不确定如何正确使用 pow 函数。根据问题的公式为:

 Monthly payment = (loan amount)(monthly interest rate) / 1.0 - (1.0 + Monthly interest rate)^-(number of months)

这是我遇到问题的行

month = (amount * rate)/1.0 -(1.0 + interest), pow(time*12);

感谢帮助

最佳答案

std::pow 像这样接受两个参数

pow (7.0,3) 

所以你的代码应该更像这样

month = (amount * rate)/1.0 - std::pow( (1.0 + interest), 12);

您的代码还有其他缺陷,比如您在设置值之前进行了计算。

关于带变量的 C++ 幂函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15320350/

相关文章:

c++ - 如何在 C++ 中创建临时文本文件?

c++ - 插槽可以接受比信号提供的参数更少的参数,如何? -Qt

c++ - 为什么 C++ 认为 8^1/3 = 1?

c++ - 如何将多个 slider 设置为相同的宽度

c++ - 什么是 undefined reference /未解析的外部符号错误,我该如何解决?

python - 如何将 pandas DataFrame 中的列提升为连续幂

c - 无符号长整数的 Pow 精度

c - C 中的 pow() 无法正常工作

javascript - 试图理解这个 math.pow 循环,不明白为什么指数必须减 1

c++ - 使用 C 或 C++ 运行本地服务器的最佳方法