简体   繁体   English

在二等分搜索中如何修复无限循环

[英]How to fix infinite loop during bisection search

My code passes test cases but if anything about ~949,000 is input it enters an infinite loop. 我的代码通过了测试用例,但是如果输入了约949,000的任何内容,则会进入无限循环。

I need to calculate the best rate at which to save a portion of a monthly income to save in order to afford a down payment in 36 months with 2 significant digits. 我需要计算保存每月收入的一部分以节省的最佳利率,以便在36个月内支付首付款(含2个有效数字)。 I'm thinking this has something to do with me not quite understanding how epsilon is calculated - I've tried calculating epsilon as 0.0001 * total_cost, 0.0004 * portion_down_payment, and 0.0001 * annual_income all to no avail. 我认为这与我不太了解epsilon的计算方式有关-我尝试将epsilon计算为0.0001 * total_cost,0.0004 * part_down_payment和0.0001 * Annual_income全部无效。

#House Hunting ps1c

low = int(0)
high = int(10000)
percent_saved = (low + high)/2.0

current_savings = 0
annual_salary = int(input("What is your starting anual salary? "))
total_cost = 1000000
semi_annual_raise = 0.07
portion_down_payment = total_cost * 0.25
epsilon = 100

r = 0.04

total_months = 0
steps = 0
while True:
    current_savings = 0
    monthly_salary = annual_salary/12
    for i in range(1,37):
        current_savings += (current_savings*r/12)
        current_savings += (monthly_salary * (percent_saved / 10000))     
        total_months += 1
        if total_months % 6 == 0:
            monthly_salary += monthly_salary * semi_annual_raise
    steps +=1   

    if abs(current_savings - portion_down_payment) <= epsilon:
        print("Steps in bisectional search: ", steps)
        best_savings_rate = str(percent_saved / 100)
        print("Best savings rate: ", (best_savings_rate + "%"))
        break
    elif (portion_down_payment - 100) - current_savings > 0:
        low = percent_saved
        percent_saved = int((low + high) / 2.0)
    else:
        high = percent_saved       
        percent_saved = int((low + high) / 2.0)

    if percent_saved >= 9999:
        print("It is not possible to afford this in 3 years")
        break

Test Case 1 测试用例1

Enter the starting salary: 150000
Best savings rate: 0.4411  
Steps in bisection search: 12 

Test Case 2 测试案例2

Enter the starting salary: 300000
Best savings rate: 0.2206 
Steps in bisection search: 9 

Test Case 3 测试案例3

Enter the starting salary: 10000
It is not possible to pay the down payment in three years

My code passes all test cases but when the input is too high it enters an infinite loop that I don't know how to reconcile. 我的代码通过了所有测试用例,但是当输入太高时,它将进入一个无限循环,我不知道该如何协调。

Essentially when annual salary becomes higher the optimal savings rates becomes smaller. 本质上,当年薪提高时,最佳储蓄率就会降低。 When the optimal savings rate becomes smaller then the level of precision you require for 当最佳储蓄率变小时,您需要的精度水平

abs(current_savings - portion_down_payment) <= epsilon

becomes higher. 变得更高。 When you cast percent_saved to an int in 当您将percent_saved转换为int

percent_saved = int((low + high) / 2.0)

it artificially limits the precision and then code enters an infinite loop. 它人为地限制了精度,然后代码进入了无限循环。

Remove the cast and the code will work always. 删除演员表,代码将始终有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM