Reputation: 31
I am trying to calculate the monthly payment of a loan and it always comes out wrong.
The formula is as follows where i is interest
((1 + i)^months /
(1 + i)^months - 1)
* principal * i
Assuming that annual interest rate and principal is an invisible floating point, can you tell me what's wrong with my formula?
double calculatePaymentAmount(int annualInterestRate,
int loanSize,
int numberOfPayments;
{
double monthlyInterest = annualInterestRate / 1200.0;
return
(
pow(1 + monthlyInterest, numberOfPayments) /
(pow(1 + monthlyInterest, numberOfPayments) - 1)
)
* (loanSize / 100)
* monthlyInterest;
}
For example: an interest rate of 1.25 and a loan size of 250 for 12 months gives 22.27 instead of 20.97.
Thank you in advance.
Edit 1: Changed monthly interest to annualInterestRate / 1200
Upvotes: 1
Views: 210
Reputation: 31
I found what was wrong. monthlyInterest = annualInterestRate / 1200.0 / 100
Upvotes: 0
Reputation: 1139
converting
double monthlyInterest = (double)annualInterestRate /
1200 / 100;
to
double monthlyInterest = (double)annualInterestRate / 12.0;
would do the trick.
you may read more about operator precedence in c from http://en.cppreference.com/w/c/language/operator_precedence
Upvotes: 0
Reputation: 23324
Assuming annualInterestRate
is in percent, then you should calculate monthlyInterest
like this:
double monthlyInterest = pow(1+(double)annualInterestRate / 100, 1/12.0) - 1.0;
Upvotes: 0