![]() |
General Programming »
Algorithms & Recipes »
Algorithms
Intermediate
Some functions for calculating loans and car leasesBy Alex EvansMaybe not exciting, but definitely handy. |
VC6, VC7Win2K, WinXP, Visual Studio, MFC, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
I know, this is probably not very exciting stuff, but may turn useful for some of you one day� Maybe for calculating your car / house repayments, or maybe writing a program that needs to calculate this.
The basis for this set of function is derived from the Microsoft Excel formula documentation on the subject and with lots of help of our own in house �Math teacher� (thanks Andrew) we managed to put this together for the benefit of the community and as our humble contribution to the forum.
Firstly � here is the Excel formula in its raw format

Now to some relevant terminology�
A �loan� is made up of several elements, each playing a different role in the equation. At times, you may need to know different things about the loan; therefore I provided here several functions.
| NPV | or Net Present Value, this is the amount of the loan |
| FV | or Future Value (or sometime known as Residual, or �Pay-Out�) is the amount of money that will still be outstanding as the final payment AFTER all the payments are made. |
| NumPay | Number of Payments / Installments, for example if a monthly payment is made for a period of 5 years, this will equal 60. |
| IntRate | Interest Rate, expressed as say 10.00 (for 10%) |
| bStart | this should be a 0 or 1, if 0, each repayment is made at the end of the period (end of month) otherwise, payments are made ahead of each period. |
OK � enough of that blurb� here are the actual functions:
double MKCalcPayment(int NumPay, double IntRate, double NPV, double FV, BOOL bStart) { IntRate /= 1200.00; double P = (- NPV * pow(1+IntRate,NumPay) + FV) / ((1 + IntRate * bStart)*((pow((1 + IntRate),NumPay) - 1) / IntRate)); return P * (-1); // Just convert it into a positive value. } double MKCalcInterest(double NumPay, double Payment, double NPV, double FV, BOOL bStart) { // Start with an arbitrary 1% Interest rate double IntRate = 1.00 / 1200.00; NPV *= (-1); double iPer = log((Payment + Payment*IntRate*bStart - FV*IntRate) / (NPV*IntRate + Payment + Payment*IntRate*bStart) ) / log (1 + IntRate); if (iPer > NumPay) { while (iPer > NumPay) { IntRate -= 0.000001; iPer = log((Payment + Payment*IntRate*bStart - FV*IntRate) / (NPV*IntRate + Payment + Payment*IntRate*bStart) ) / log (1 + IntRate); } } else { while (iPer < NumPay) { IntRate += 0.000001; iPer = log((Payment + Payment*IntRate*bStart - FV*IntRate) / (NPV*IntRate + Payment + Payment*IntRate*bStart) ) / log (1 + IntRate); } } return IntRate * 1200.00; } double MKCalcPeriods(double Payment, double IntRate, double NPV, double FV, BOOL bStart) { IntRate /= 1200.00; NPV *= (-1); return log((Payment + Payment*IntRate*bStart - FV*IntRate) / (NPV*IntRate + Payment + Payment*IntRate*bStart) ) / log (1 + IntRate); } double MKCalcResidual(double Payment, int NumPay, double IntRate, double NPV, BOOL bStart) { IntRate /= 1200.00; return -Payment * ((1 + IntRate * bStart)*((pow((1 + IntRate),NumPay) - 1) / IntRate)) + (NPV * pow(1+IntRate,NumPay)); }
All of the above functions are arithmetic juggling of the original formula,
with the exception of the one named MKCalcInterest which does a bit
of �Guessing� as to what the Interest rate is for a set of given arguments.
If you read the Excel on line help you will see that there is no direct way of
finding the answer otherwise.
MKCalcPayment function results in $531.18 per
month.
MKCalcPeriods shows that we now have
to pay 88.65 payments instead of 60�
MKCalcResidual returns
this to be equal to $10,157.89. Well it seems you cant have it all � you
either pay more each month, or you pay for a longer period, or you are left
out with something to pay at the end�
MKCalcInterest
function � the answer is 11.52% per annum� This means that you should be
paying less than 60 installments � otherwise you are paying too much. See
why by looking at the 1st example.Well, I am just about done, you can play with more examples � paying ahead or in arrears, with or without a residual etc..
Have fun
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 10 Mar 2003 Editor: Chris Maunder |
Copyright 2003 by Alex Evans Everything else Copyright © CodeProject, 1999-2009 Web20 | Advertise on the Code Project |