Calculating Loan Payments: Rounding Correctly?

  • Thread starter newageanubis
  • Start date
In summary: Depending on the compiler and the floating point representation of the number, there are an infinite number of potential rounding errors.
  • #1
newageanubis
15
0
Hey everyone,

I'm writing a simple program to calculate the monthly payment amount for a loan given a principal, down payment, finance term, and interest rate. The monthly payment needs to be reported to the user rounded to two decimal places after they have entered all the required information. The assignment instructions specify that the monthly payment should be rounded to two decimal places, not truncated, floored, ceiled (?), etc.

To accomplish this, I have used the conversion specification in the printf statement to specify that the monthly payment should be printed to two decimal places:

"printf("The monthly payment amount is: %.2lf", downPayment);"

Does this method round properly, or does it just truncate? Will it produce the same results as multiplying the monthly payment by 100, using the round function from math.h to round the result, and then dividing by 100?

Thanks!
 
Technology news on Phys.org
  • #2
What you show doesn't do any rounding. It's just truncating the result after the 2nd decimal place. So no, it doesn't produce the same results as actual rounding.
 
  • #3
Depending on the compiler, printf output will generally round floating point numbers, but I don't think there's any rule in C / C++ that requires rounding. Microsoft compilers will round with printf.
 
  • #4
rcgldr said:
Depending on the compiler, printf output will generally round floating point numbers, but I don't think there's any rule in C / C++ that requires rounding. Microsoft compilers will round with printf.
The standards are fairly tight with respect to rounding.

However, there is an issue here. If your computer is using the IEEE floating point standard (almost every computer nowadays), a floating point number doesn't represent just one number. It instead represents a range of numbers. Every number in that range will have the same IEEE representation. Consider the two numbers 19810222/219 + 21474836/247 and 37785/1000. These two numbers have the same representation in the IEEE double precision format. The first number is "exact"; it's IEEE representation is exactly equal to the 19810222/219 + 21474836/247. The latter number, 37785/1000 (or 37.785), is not an exact number.

Consider the following C code:
Code:
#include <stdio.h>
int main () {
   double x1, x2;
   long int two_pow19 = 1L << 19;
   long int two_pow47 = 1L << 47;

   x1 =  37785.0 / 1000.0;
   printf ("37.785 rounded = %.2f\n", x1);

   x2 = 19810222.0/two_pow19 + 21474836.0/two_pow47;
   printf ("19810222/2^19 + 21474836/t2^47 rounded = %.2f\n", x2);

   printf ("x1-x2 = %.15g\n", x1-x2);

   printf ("x1*2^47 = %.2f\n", x1*two_pow47);

   return 0;
}

Even if you are of the opinion that 37.785 should be rounded to 37.79 (opinions do vary, and so do different versions of the C library), most computers will print 37.785 as 37.78. That's because the exact number in the range of numbers that have the same representation is 19810222/219 + 21474836/247, or approximately 37.7849999999999966. Assuming you are using the default rounding mode, that number should be rounded down to 37.78. So it is -- even if you meant 37.785 and even if you want those corner cases to be rounded up.

Now consider the following Excel formulae in two cells:
Code:
=ROUND(37785/1000,2)
=ROUND(19810222/2^19+21474836/2^47,2)

Excel will display 37.79 as the value of both cells. Excel looks to see whether the range of numbers represented by some IEEE representation contains the corner case, in this case 37.785. That corner case rounds up per Excel's rounding rules, so 37.785 is rounded up to 37.79, but so is 37.7849999999999966.
 
  • #5
yeah, I thought that was always the case...%W.df rounds to d decimal places...that's what I have always assume in tcl, C, fortran, python, java...
 
  • #6
If you want to round to 2 decimal places, you can add .005 then truncate it. You should be able to generalize it to what ever precision you want.
 
  • #7
e.g:- rounding of 10.457 to the nearest 2 decimal places

(integer(10.457*100+0.5))/100 = 1046/100 = 10.46

for 3 decimal places *1000 and then /1000
 
Last edited:
  • #8
Do that with my example of 37.785.

Addendum
Or, for that matter, if your computer is as goofy as mine (and it almost certainly is), try 0.145, 0.285, 0.565, and 0.575. The next problem child doesn't occur until after 1. In the 1s, you have 1.005, 1.015, 1.025, 1.035, 1.125, 1.135, 1.145, 1.155, 1.255, 1.265, and 1.275. And so on.

Rounding isn't quit as simple as multiplying by 10n, adding 1/2, casting to an integer, and dividing by 10n. At least not if you want to do it right.
 
Last edited:

1. What is the purpose of rounding correctly when calculating loan payments?

Rounding correctly is important in calculating loan payments because it ensures accuracy and eliminates errors in the final calculation. It also helps to maintain consistency in the calculation process and provides a more precise estimate of the loan payment amount.

2. When should rounding be applied in the loan payment calculation?

Rounding should be applied at the end of each step in the loan payment calculation, rather than at the end of the entire calculation. This ensures that the rounded numbers are used in the subsequent steps, leading to a more accurate final result.

3. How many decimal places should be used when rounding in loan payment calculations?

The number of decimal places used for rounding in loan payment calculations is usually dependent on the currency being used. For example, in the United States, two decimal places are typically used (e.g. $123.45). However, it is important to follow the specific guidelines and regulations of the governing body or institution.

4. Is it necessary to round up or down when calculating loan payments?

In most cases, it is recommended to round up when calculating loan payments. This is because rounding down can result in a lower final payment amount, which may not accurately reflect the total amount owed. However, it is important to follow any specific rounding rules set by the governing body or institution.

5. What are the potential consequences of not rounding correctly in loan payment calculations?

Not rounding correctly in loan payment calculations can lead to errors in the final result, which can result in incorrect payment amounts being made or received. This can cause financial discrepancies and potentially impact the borrower's ability to repay the loan. Additionally, not following specific rounding guidelines set by governing bodies or institutions can result in penalties or legal consequences.

Similar threads

  • Programming and Computer Science
Replies
7
Views
4K
  • Programming and Computer Science
Replies
1
Views
2K
  • Precalculus Mathematics Homework Help
Replies
14
Views
6K
  • General Math
Replies
2
Views
1K
Replies
12
Views
3K
  • Introductory Physics Homework Help
Replies
3
Views
430
  • Programming and Computer Science
Replies
8
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
10K
  • Programming and Computer Science
Replies
5
Views
5K
Replies
6
Views
3K
Back
Top