1

私はこの式を機能させようとしています。すべてを数えたので、括弧ではないと確信しています。おそらく、パラメータ pow (x,y) に関して何か間違っていることがあります。

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

 return periodicPaymentcalc;
}
4

3 に答える 3

9

各ステップを細かく分割すると、関数が何をしているのかを理解するのがどれほど簡単になるかに注意してください: (変数が元の資料と一致するとさらに簡単になるので、ウィキペディアが使用するものにちなんで変数に名前を付けます. )

// 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

このグループ化では、1 つの引数のみを に渡します。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おそらく、関数で 2 回使用し、両方の場所で同じテンポラリを使用することに気付くなど、コンパイラが使用するいくつかの最適化がありますが、これが発生するという保証はありません。両方の関数でほぼ同じ数の変数を使用していることに注意してください。名前付き変数を増やし、名前のない一時変数を減らしただけです。

于 2010-03-16T21:30:25.257 に答える
2

ブラケットの位置が間違っています。修正版は次のとおりです。

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

この種のエラーを回避するには、対応する括弧を強調表示するエディターを使用してください。または、一時変数を作成して中間値を保持します。

于 2010-03-16T20:50:28.020 に答える
1
periodicPaymentcalc = (loan * interestRate / yearlyPayment) /
  (1.0 - pow (1.0 + interestRate / yearlyPayment, -yearlyPayment * numOfYearLoan));

それを試してみてください。余計な括弧もすべて削除し、すべてのリテラルをdoubleに変更しました。

于 2010-03-16T20:57:57.503 に答える