Problem 34 Binary search precision

Back to Problem Solutions forum

SirTurtleton     2017-08-18 07:17:39

Hi, I'm doing binary search with python. The results seem to be fairly correct up to a point due to imprecision. I suspect it's either from using float type in the equation or the condition of while loop. I feel strongly about the latter. Could anyboy please give me some pointers to fix this? Thank you n advance.

goto     2017-08-18 10:48:50

Answer requires 0.0000001 precision and your while loop stops after reaching abs(result) >= 1. You can use floating point in the conditions as long as you are not trying to test their equality.

SirTurtleton     2017-08-18 11:00:02

Could you show me how it works? I'm not yet clear on this.

goto     2017-08-18 11:40:55

Try abs(result) > 0.0000001 instead of abs(result) >= 1.

SirTurtleton     2017-08-18 11:47:56

Wow, that works! Thank you. So the gist is the result must be within 0.0000001 of 0?

goto     2017-08-18 13:36:14

0.0000001 precision means that all inequalities between calculations should be below 0.0000001 . For example if your result is 0.123456789 and after next binary operation it becomes 0.12344455, you need to continue calculating the answer until first 7 digits after decimal point doesn't change anymore.

SirTurtleton     2017-08-18 15:32:54

I think I kinda understand the idea of what you said. Is there any source where I can read up on this, if you don't mind sharing? Also, for the line while abs(result) > 0.0000001 means that the loops continues as long as value of result is greater than 0.0000001? Sorry to be a bother, I believe that I missed this in school.

Please login and solve 5 problems to be able to post at forum