Поиск пополам в Python не могу найти ответ? Почему это приводит к отрицательному балансу? - PullRequest
0 голосов
/ 07 ноября 2018

Этот код возвращается к -73535 и выходит из цикла. Я не могу понять, что не так.

balance = 999999   
annualInterestRate = 0.18   
epsilon = 0.05

monthlyInterestRate = annualInterestRate / 12    
lowerBound = balance / 12    
upperBound = (balance * ((1 + monthlyInterestRate)**12)) / 12.0    
print(upperBound, lowerBound)   
currentBalance = balance    
middle = (upperBound + lowerBound) / 2   

while abs(round(upperBound - lowerBound,2)) >= epsilon:    
    currentBalance = balance
    for i in range(12):
        unpaidBalance = currentBalance - middle
        currentBalance= unpaidBalance - (monthlyInterestRate * unpaidBalance)
    print(currentBalance)
    if abs(unpaidBalance) < 0.05:
        print("hahaha")
        break
    elif currentBalance < 0:
        upperBound = middle
    elif currentBalance > 0:
        lowerBound = middle
    middle = (upperBound + lowerBound) / 2
...