473,378 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Incorrect value being returned after calculation.

I am writing a program that calculates the monthly payment and interest of a loan. I am having trouble getting the program to calculate the Monthly Payment. The correct formula given by my textbook is as follows:

Payment =
Rate * (1 + Rate)^N
---------------------------- * L
( (1 + Rate)^N - 1)
Rate, being the monthly interest rate
N, being the number of payments
L, being the loan amount.

Expand|Select|Wrap|Line Numbers
  1. MonthlyIntRate = AnnualIntRate / 12.0;
  2.     monthlyPayment = ((MonthlyIntRate * pow(1 + MonthlyIntRate, numPayments)) / (pow(1 + MonthlyIntRate, numPayments) - 1.0)) * loanAmount;
  3.  
Mar 22 '07 #1
7 1667
sicarie
4,677 Expert Mod 4TB
I am writing a program that calculates the monthly payment and interest of a loan. I am having trouble getting the program to calculate the Monthly Payment. The correct formula given by my textbook is as follows:

Payment =
Rate * (1 + Rate)^N
---------------------------- * L
( (1 + Rate)^N - 1)
Rate, being the monthly interest rate
N, being the number of payments
L, being the loan amount.

Expand|Select|Wrap|Line Numbers
  1. MonthlyIntRate = AnnualIntRate / 12.0;
  2.     monthlyPayment = ((MonthlyIntRate * pow(1 + MonthlyIntRate, numPayments)) / (pow(1 + MonthlyIntRate, numPayments) - 1.0)) * loanAmount;
  3.  
Have you tried breaking the individual calculations up into several lines, to make sure order of operations is how you want it?
Mar 22 '07 #2
No....I am not sure how to break up a formula on several lines....got any examples?
Mar 22 '07 #3
sicarie
4,677 Expert Mod 4TB
No....I am not sure how to break up a formula on several lines....got any examples?
You might want to talk to your teacher then, that could be a problem.

Take the individual calculations (addition, subtraction, multiplication, powers...), and do them individuallty

Example:
int = 1 + 2 * 3;

to
int = 2*3;
int += 1;
Mar 22 '07 #4
Have you tried breaking the individual calculations up into several lines, to make sure order of operations is how you want it?
So am I on the right track with this formula below:
Expand|Select|Wrap|Line Numbers
  1. monthlyPayment = MonthlyIntRate * pow(1 + MonthlyIntRate, numPayments)
  2. monthlyPayment /= (pow(1 + MonthlyIntRate, numPayments) - 1.0)
  3. monthlyPayment *= loanAmount;
  4.  
Mar 22 '07 #5
sicarie
4,677 Expert Mod 4TB
So am I on the right track with this formula below:
Expand|Select|Wrap|Line Numbers
  1. monthlyPayment = MonthlyIntRate * pow(1 + MonthlyIntRate, numPayments)
  2. monthlyPayment /= (pow(1 + MonthlyIntRate, numPayments) - 1.0)
  3. monthlyPayment *= loanAmount;
  4.  
I duno, did that help?

I would start with :
Expand|Select|Wrap|Line Numbers
  1. montlyPayment = pow(1+monthlyIntRate,numPayments);
  2. cout << monthlyPayment;
  3. montlyPayment *= MontlyIntRate;
  4. cout << montlypayment;
  5. ....
  6.  
and literally do each calculation separately, printint out after each (that way you can debug/check the values).

From there, you can combine them slowly until you see where the errors are coming from (I'm just guessing order of operations).
Mar 22 '07 #6
So am I on the right track with this formula below:
Expand|Select|Wrap|Line Numbers
  1. monthlyPayment = MonthlyIntRate * pow(1 + MonthlyIntRate, numPayments)
  2. monthlyPayment /= (pow(1 + MonthlyIntRate, numPayments) - 1.0)
  3. monthlyPayment *= loanAmount;
  4.  




Tried the above formula and it is not working.....
Mar 22 '07 #7
sicarie
4,677 Expert Mod 4TB

I would start with :
Expand|Select|Wrap|Line Numbers
  1. montlyPayment = pow(1+monthlyIntRate,numPayments);
  2. cout << monthlyPayment;
  3. montlyPayment *= MontlyIntRate;
  4. cout << montlypayment;
  5. ....
  6.  
and literally do each calculation separately, printint out after each (that way you can debug/check the values).

From there, you can combine them slowly until you see where the errors are coming from (I'm just guessing order of operations).
Did you try this?
Mar 22 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: John | last post by:
When I create a view consisting of an inner join between a view and a table the columns refernced in the the view are returned incorrectly, example select id.itemcode, id.description,...
5
by: Christoph | last post by:
alert( 100 * 1.15 ); The above *should* show a value of 115. However, what I am actually getting is: 114.99999999999998 alert( 100 * 1.10 ); is showing as:
3
by: Annette Massie | last post by:
I am trying to insert a record into a table via code and one of the values to add I would like as a dateadd calculation on a value from a query. My code looks like this: Set db = CurrentDb() ...
6
by: lovecreatesbeauty | last post by:
I ever missed a `return' statement when write a function `int HighDigit(Num)' to get the highest digit of an integer. But even if the `return' statement is ignored the function still can obtain...
9
by: Coleen | last post by:
Hi All :-) I found the way to get my column sum (Thanks Cor I did it a little different, but the result is what I wanted) I used: dt_stat_report_3b.Columns.Add(New DataColumn("Sum",...
2
cassbiz
by: cassbiz | last post by:
I may be in the wrong forum so Ronald don't shoot :) In my code I have an option box to choose a number - works fine. I want to carry over the new value to another field to do a recalculation. ...
1
by: `Zidane Tribal | last post by:
it would appear that using the command 'crc("data")' from the String::CRC returns incorrect results (although, they are at least consistently incorrect). for example, this script..... ...
21
by: mdh | last post by:
Hi Group, I know there have been many questions on this topic, but I wish to clarify this very basic concept. in: int main(){ int j; j = f(8); .....}
5
by: =?Utf-8?B?QW5kcmV3?= | last post by:
Hi, What is wrong with this code ? I got this error: " Line 1: Incorrect syntax near 'sp_Collect'. " which appears at the last line when | call the cm.ExecuteScalar . The sp takes an int ID...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.