tags:

views:

172

answers:

3

I'm trying to get this expression to work, I'm pretty sure its not the parenthesis because I counted all of them. Perhaps there something I'm doing wrong involving the parameter pow (x,y).

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

 return periodicPaymentcalc;
}
+2  A: 

There's a misplaced bracket. Here's a fixed version:

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

Use an editor that highlights matching brackets to avoid this kind of errors. Or simply create temporary variables to hold intermediate values.

Max Shawabkeh
Still same error
Sagistic
Or writing out the calculation using more than one line of code, in the case that temporary variables are not an option for some reason.
Brian
I guess, I'll just have to use temp values and simplify it
Sagistic
@Sagistic: What do you mean "I guess"? It's completely ugly on a single line. There is no loss in splitting it up, only a gain in readability.
GMan
+1  A: 
periodicPaymentcalc = (loan * interestRate / yearlyPayment) /
  (1.0 - pow (1.0 + interestRate / yearlyPayment, -yearlyPayment * numOfYearLoan));

Try that. I removed all the redundant parentheses too, as well as changing all literals to doubles, just for good measure.

Peter Alexander
+9  A: 

Notice how much easier it is to figure out what the function is doing if you break each step up into pieces: (I find it even easier if your variables match the source material, so I'll name my variables after the ones Wikipedia uses.)

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

It's much easier to confirm that the logic of this function does what it should this way.

If you're curious, substituting my variable names in, your parenthises problem is as follows:

  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

With this grouping, you're only passing one argument to pow, which is why the compiler says no overloaded function takes 1 arguments.

Edit: You mentioned I used more variables. However, your compiler will use temporary variables much like I did. Your complex statement will be broken up into pieces, and may look something like this:

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; 
} 

Mine will also be broken up, and will look like:

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; 
} 

Perhaps there are some optimizations that the compiler will use, such as noticing that it uses interestRate / yearlyPayment twice in your function, and use the same temporary for both places, but there's no gurantee this will happen. Notice that we use pretty much the same number of variables in both of our functions. I just used more named variables, and fewer unnamed temporaries.

Bill
+1: nice answer, with good guidance on how to write readable code
Paul R
Thank you very much. I couldn't get it to run in a single line, so I have seperated into smaller steps, but with more variables.
Sagistic
@Sagistic: Glad I could help! It's important to realize though that I'm not using any more variables than you were. Yours were just unnamed variables used by the compiler to computer intermidiate results. I'll edit my post to elaborate.
Bill
@ Bill: Wow!! So nicely written. I ended up writing mine sort of hard to understand. MANY thanks to you! I sincerely appreciate you taking the time to fully explain it to me.
Sagistic