Python 3.x 计算理想储蓄率的二分法搜索实现误差

Python 3.x 计算理想储蓄率的二分法搜索实现误差,python-3.x,algorithm,loops,bisection,Python 3.x,Algorithm,Loops,Bisection,我一直试图在MIT OCW 6.0001(Python中的CS简介)PSET 1 C部分中实现一个对分搜索算法,但它不起作用。我们的目标是根据用户的输入工资(在我的代码中定义了固定变量)找到理想的储蓄率,以支付新房子的首期付款。我不熟悉二分法搜索,并且正在努力实现这段代码 semi_annual_raise = 0.07 r = 0.04 total_cost = 1000000 down_payment = 0.25 * total_cost annual_salary = int(input

我一直试图在MIT OCW 6.0001(Python中的CS简介)PSET 1 C部分中实现一个对分搜索算法,但它不起作用。我们的目标是根据用户的输入工资(在我的代码中定义了固定变量)找到理想的储蓄率,以支付新房子的首期付款。我不熟悉二分法搜索,并且正在努力实现这段代码

semi_annual_raise = 0.07
r = 0.04
total_cost = 1000000
down_payment = 0.25 * total_cost
annual_salary = int(input("Enter your annual salary: "))
current_savings = 0
epsilon = 100
low = 0
high = 10000
savings_rate = ((high + low)/2)
num_guesses = 0

#to find: best rate of savings to achieve the money

month = 0

while current_savings <= down_payment - epsilon and month < 37:
    if (month != 0 and month % 6 == 0):
        annual_salary += annual_salary*semi_annual_raise
    current_savings += ((annual_salary/12 *savings_rate/(10**4)) + current_savings*r/12)
    if current_savings < down_payment - epsilon:
        low = savings_rate
    else:
        high = savings_rate
    savings_rate = (high + low)/2
    num_guesses+=1
    month += 1
        
if (month > 36 or current_savings < down_payment - epsilon):
    print("Sorry, can't afford that")
else:
    print("Best savings rate: ", savings_rate/10**4)
    print("Steps in bisection search: ", num_guesses)
我的代码的输出:

Enter your annual salary: 150000
Best savings rate:  0.9999971389770508
Steps in bisection search:  19
问题是,根据我实现对分搜索的方式,我可以得到与理想输出语句稍有不同的值。但我真的很难理解这意味着什么

Enter your annual salary: 150000
Best savings rate:  0.9999971389770508
Steps in bisection search:  19