各ステップを細かく分割すると、関数が何をしているのかを理解するのがどれほど簡単になるかに注意してください: (変数が元の資料と一致するとさらに簡単になるので、ウィキペディアが使用するものにちなんで変数に名前を付けます. )
// 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 回使用し、両方の場所で同じテンポラリを使用することに気付くなど、コンパイラが使用するいくつかの最適化がありますが、これが発生するという保証はありません。両方の関数でほぼ同じ数の変数を使用していることに注意してください。名前付き変数を増やし、名前のない一時変数を減らしただけです。