【问题标题】:C++ Question on the pow function关于 pow 函数的 C++ 问题
【发布时间】:2010-03-16 20:46:22
【问题描述】:

我正在尝试使这个表达式起作用,我很确定它不是括号,因为我计算了所有这些。也许我做错了涉及参数 pow (x,y)。

double calculatePeriodicPayment()
{
 periodicPaymentcalc = (loan * ((interestRate / yearlyPayment)))  / (1-((pow ((1+(interestRate / yearlyPayment)))),(-(yearlyPayment * numOfYearLoan))));

 return periodicPaymentcalc;
}

【问题讨论】:

  • 如果你把计算分成更小的步骤,也许它会更易读?
  • 请提供您遇到的错误/不当行为。
  • 在什么情况下它不起作用?编译错误?运行时结果错误?运行时错误?如果是错误,请发布错误。此外,我们可能需要知道所有这些变量的类型。
  • 我最担心的是我是否使用了错误的 pow 表达式。我得到的唯一编译器错误是:error C2661:'pow':没有重载函数需要 1 个参数
  • 我必须支持“更小步骤”的建议。将所有内容放在一条线上没有任何好处,但正如您发现的那样,有足够的空间犯错。在内部,编译器无论如何都会生成一堆临时值,那么为什么不让函数更具可读性呢?

标签: c++ exponent


【解决方案1】:

请注意,如果您将每个步骤分解为多个部分,那么找出函数正在做什么会变得多么容易: (如果您的变量与源材料匹配,我发现它会更容易,所以我会以维基百科使用的变量命名我的变量。)

// amortization calculator
// uses annuity formula (http://en.wikipedia.org/wiki/Amortization_calculator)
// A = (P x i) / (1 - pow(1 + i,-n))
// Where:
//   A = periodic payment amount
//   P = amount of principal
//   i = periodic interest rate
//   n = total number of payments
double calculatePeriodicPayment()
{ 
  const double P = loan;
  const double i = interestRate / yearlyPayment;
  const double n = yearlyPayment * numOfYearLoan;

  const double A = (P * i) / (1 - pow(1.0 + i, -n));

  return A; 
} 

这样更容易确认这个函数的逻辑做了它应该做的事情。

如果你好奇,把我的变量名代入进去,你的括号问题如下:

  const double A = (P * i) / (1 - pow(1 + i)), -n; // <- this is how you have it
  const double A = (P * i) / (1 - pow(1 + i, -n)); // <- this is how it should be

使用此分组,您只需将一个参数传递给pow,这就是编译器显示no overloaded function takes 1 arguments 的原因。

编辑:你提到我使用了更多变量。但是,您的编译器将像我一样使用临时变量。您的复杂语句将被分解成多个部分,可能看起来像这样:

double calculatePeriodicPayment() 
{
  const double temp1 = interestRate / yearlyPayment;
  const double temp2 = loan * temp1;
  const double temp3 = interestRate / yearlyPayment;
  const double temp4 = 1.0 + temp3;
  const double temp5 = yearlyPayment * numOfYearLoan;
  const double temp6 = -temp5;
  const double temp7 = pow(temp4, temp5);
  const double temp8 = 1 - temp7;
  const double temp9 = temp2 / temp8;

  periodicPaymentcalc = temp9; 
  return periodicPaymentcalc; 
} 

我的也会被打散,看起来像:

double calculatePeriodicPayment()
{ 
  const double P = loan;
  const double i = interestRate / yearlyPayment;
  const double n = yearlyPayment * numOfYearLoan;

  const double temp1 = P * i;
  const double temp2 = 1.0 + i;
  const double temp3 = -n;
  const double temp4 = pow(temp2, temp3);
  const double temp5 = 1 - temp4;
  const double temp6 = temp1 / temp5;
  const double A = temp6;

  return A; 
} 

也许编译器会使用一些优化,比如注意到它在你的函数中使用了两次interestRate / yearlyPayment,并在两个地方使用了相同的临时值,但不能保证会发生这种情况。请注意,我们在两个函数中使用了几乎相同数量的变量。我只是使用了更多的命名变量,更少的未命名的临时变量。

【讨论】:

  • +1:很好的答案,对如何编写可读代码有很好的指导
  • 非常感谢。我无法让它在一行中运行,所以我分成了更小的步骤,但变量更多。
  • @Sagistic:很高兴我能帮上忙!重要的是要意识到我没有使用比你更多的变量。你的只是编译器用来计算中间结果的未命名变量。我将编辑我的帖子以详细说明。
  • @比尔:哇!!写得真好。我最终写了我的有点难以理解。非常感谢你!非常感谢您抽出宝贵时间向我详细解释。
  • i 和 n 始终是整数类型(其中 i 是循环变量)。至少在我看来。 ;) 不过,一个干净的解决方案!
【解决方案2】:

有一个错位的括号。这是一个固定版本:

periodicPaymentcalc = (loan * ((interestRate / yearlyPayment))) / (1 - ((pow ((1+(interestRate / yearlyPayment)),(-(yearlyPayment * numOfYearLoan))))));

使用高亮匹配括号的编辑器来避免此类错误。或者只是创建临时变量来保存中间值。

【讨论】:

  • 或者使用多行代码写出计算,以防由于某种原因临时变量不是一个选项。
  • 我想,我只需要使用临时值并简化它
  • @Sagistic:“我猜”是什么意思?它在一条线上完全丑陋。拆分没有损失,只有可读性增加。
【解决方案3】:
periodicPaymentcalc = (loan * interestRate / yearlyPayment) /
  (1.0 - pow (1.0 + interestRate / yearlyPayment, -yearlyPayment * numOfYearLoan));

试试看。我也删除了所有多余的括号,并将所有文字更改为双精度值。

【讨论】:

    猜你喜欢
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多