0

I have to create a code to find the exact payment, to the cent, needed to pay off a loan in 12 months using bisection. The code I created for this works but it overshoots it's target. The loan will be payed off within the 12 months but after making 12 payments the final balance should be around 0. However it is a way bigger negative number.

The code I am using is:

StartBalance = float(raw_input('Credit Balance in $: '))
AnnualRate = float(raw_input('Annual interest rate in decimals: '))
MonthlyRate = AnnualRate / 12.0
MinPaymentLow = StartBalance / 12.0
MinPaymentHigh = (StartBalance*(1+MonthlyRate)**12.0)/12.0
cent = 0.01
Payment = (MinPaymentHigh+MinPaymentLow)/2.0

while (Payment*12-StartBalance) >= cent:
    for month in range(0, 12):
        Balance = (StartBalance-Payment)/10*(1+MonthlyRate)   
    if Balance < 0:
        MinPaymentLow = Payment
    elif Balance > 0:
        MinPaymentHigh = Payment
    Payment = (MinPaymentHigh + MinPaymentLow)/ 2.0

print 'RESULT'           
print 'Number of months needed: 12'
print 'Montly pay: $', round(Balance,2)
Pim
  • 45
  • 1
  • 1
  • 3
  • How are you testing whether the value you produce is correct? It seems likely you're using a different loan/interest calculation than whatever you're testing against. – James Sep 04 '13 at 15:14
  • I haven't gotten around to code a piece to test it but I used an online debtcalculator and checked it manually. The problem is every month you pay off for example $150, then the last month you will have $50 of debt left to pay, but you will pay off $150. This leaves you with an account balance of -$100 whilst it should be $0 (or close to zero). – Pim Sep 04 '13 at 18:00
  • I fixed it by creating a different condition for the while loop and by setting the if and elif statements as (balance <0 and balance <-cent) and vica versa. Thanks for the help though. – Pim Sep 05 '13 at 10:02

2 Answers2

0

Your code seemed to work fine for me, but if you're getting results that are "way off" it's probably because you're using the float datatype. Float is untrustable, because it rounds on every operation. Given enough iterations and you've rounded off a lot of money. Try using decimal instead. This module keeps track of decimals as indexed integer values.

blakev
  • 4,154
  • 2
  • 32
  • 52
  • Thanks for your input! I can use the decimal instead of float? Because when I try to substitude decimal for float it gives me an error. The results I'm getting are sometimes just as far off as the monthly payment, seems a bit much for a rounding error but that could be just me. – Pim Sep 04 '13 at 18:02
0

It looks like these lines:

for month in range(0, 12):
    Balance = (StartBalance-Payment)/10*(1+MonthlyRate)   

Should be:

Balance = StartBalance
for month in range(0, 12):
    Balance = (Balance-Payment) * (1 + MonthlyRate)  

Or something similar, depending on how you want to calculate interest, and whether you consider payments occurring at the start or end of the month.

James
  • 24,676
  • 13
  • 84
  • 130