Program is rounding the decimal value wrong!

Hey guys I am using an example out of a book that shows sample output as the following.

Loan Amount: $ 10000.00
Monthly Interest Rate: 1%
Number of Payments: 36
Monthly Payment: $ 332.14
Amount Paid Back: $ 11957.15
Interest Paid: $ 1957.15

Whenever I input those same values. I get my Amount Paid Back as 11957.16 and Interest paid as 1957.16
I've come across a problem similar to this in the past. I know that is due to in accurate rounding that occurs in programming. How can I fix this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
	//float variables to hold data that the user will input
	float LoanAmount, AnnualInterestRate, NUMofPayments, MonthlyRate, TotalMonthlyPayment, Power,
		PaidBack, InterestPaid;
	
	//Prompting user to enter input of data
	cout << "Please enter a loan amount ($): ";
	cin >> LoanAmount;

	if (LoanAmount < 0)//if statements to determine whether non-negative value was entered
	{
		cout << "ERROR! Negative number was entered!" << endl;
		return 0;
	}

	cout << "Please enter an annual interest rate (%): ";
	cin >> AnnualInterestRate;
	
	if (AnnualInterestRate < 0)//if statements to determine whether non-negative value was entered
	{
		cout << "ERROR! Negative number was entered!" << endl;
		return 0;
	}

	cout << "Enter term length of loan (Months): ";
	cin >> NUMofPayments;

	if (NUMofPayments < 0)
	{
		cout << "ERROR! Negative number was entered!" << endl;
		return 0;
	}
	
	//Calculations for the formula
	MonthlyRate = (((AnnualInterestRate/12.0F)/100.0F));

	//powf is used for the power function for exponents when using float values
	Power = powf(1.0F + MonthlyRate, NUMofPayments);

	//formula for the total monthly payment
	TotalMonthlyPayment = ((MonthlyRate * Power)/(Power - 1)) * LoanAmount;

	//calculating the total with interest
	PaidBack = (TotalMonthlyPayment * NUMofPayments);
	//calculating the amount of interest that needs to be paid
	InterestPaid = PaidBack - LoanAmount;

	cout << "\nLoan Amount: " << setw(15) << "$ " << fixed << setprecision(2) << LoanAmount << endl;
	cout << "Monthly Interest Rate: " << setw(12) << MonthlyRate * 100 << "%" << endl;
	cout << "Number of Payments: " << setw(16) << setprecision(0) <<NUMofPayments << endl;
	cout << "Monthly Payment: "  << setw(11) << "$ " << fixed << setprecision(2) << setw(8) << TotalMonthlyPayment << endl;
	cout << "Amount Paid Back: " << setw(10) << "$ " << setw(7) << PaidBack << endl;
	cout << "Interest Paid: " << setw(13) << "$ " << setw(8) << InterestPaid << endl;

	return 0;
}
The monthly interest rate is not the annual interest rate divided by twelve.
1
2
3
4
float percent_annual_rate = /*input*/;
float unit_annual_rate = (percent_annual_rate + 100) / 100;
float unit_monthly_rate = powf(float unit_annual_rate, 1.f/12.f);
float percent_monthly_rate = unit_monthly_rate * 100 - 100;
So, for example, an annual rate of 20% (1.2) is a monthly rate of 1.53% (1.0153). Not 1.67% (1.0167).
Last edited on
Unless there is a good reason not to do so, use double as the floating point type.

Change float to double everywhere - variables, literals(12.0 instead of 12.0F), and functions (std::pow instead of powf - and you would get the more accurate result.

http://coliru.stacked-crooked.com/a/f06f91deacf56bde
http://rextester.com/ESPK55303
The monthly interest rate is not the annual interest rate divided by twelve.

But it is? That's the way I've always been taught to do it when solving annuity problems. I worked out the problem by hand and got the same answer as OP.

Whenever I input those same values. I get my Amount Paid Back as 11957.16 and Interest paid as 1957.16
I've come across a problem similar to this in the past. I know that is due to in accurate rounding that occurs in programming. How can I fix this?

I believe it's because you're using floats whose precision is easily exceeded. I changed everything to be a double and got the answer as expected.

Please enter a loan amount ($): 10000
Please enter an annual interest rate (%): 12
Enter term length of loan (Months): 36

Loan Amount:              $ 10000.00
Monthly Interest Rate:         1.00%
Number of Payments:               36
Monthly Payment:          $   332.14
Amount Paid Back:         $ 11957.15
Interest Paid:            $  1957.15

You also forgot to #include <cmath>.
That's the way I've always been taught to do it when solving annuity problems.
You've been taught wrong.

Suppose that you take out a loan for $1000 with a monthly interest rate of 1%. That means that every month you current debt is multiplied by 1.01.
Before the first month, your debt is $1000.
After the first month, your debt is $1010.
After the second month, your debt is $1020.1.
After the third month, your debt is $1030.301.
After the nth month, your debt is $1000 * 1.01^n.
After the twelfth month, your debt is $1126.83. The annual interest rate is 1.01^12 = 1.127. If you just divide this number by 12, you get 1.0106, which is of course higher than 1.01.
You have to take the twelfth root of the annual interest rate to get the monthly interest rate.
Last edited on
> That's the way I've always been taught to do it when solving annuity problems.

Yes, because the commonly stated annual interest rate (for loans etc.) in most countries is the APR (Annualised Percentage Rate) with a compounding period of a month.

If the rate is specified to be the EAPR (Effective APR), the computation would be different.

The nominal interest rate (also known as an Annualised Percentage Rate or APR) is the periodic interest rate multiplied by the number of periods per year. For example, a nominal annual interest rate of 12% based on monthly compounding means a 1% interest rate per month (compounded).
...
Nominal interest rates are not comparable unless their compounding periods are the same; effective interest rates correct for this by "converting" nominal rates into annual compound interest. In many cases, depending on local regulations, interest rates as quoted by lenders and in advertisements are based on nominal, not effective interest rates, and hence may understate the interest rate compared to the equivalent effective annual rate.

https://en.wikipedia.org/wiki/Nominal_interest_rate#Nominal_versus_effective_interest_rate
Hey all,

Thanks for your responses. I have to use floating values. This is because of the specific instructions I received. Otherwise I would have no issue using doubles as I personally prefer them.
As for the monthly rate, that is how I was told to do the rate. It says to convert the annual to monthly by dividing by 12.

So how can I go about fixing the rounding issue with floats?

Thanks,
MisterTams
Topic archived. No new replies allowed.