Back to Problem Solutions forum
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.
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.
Could you show me how it works? I'm not yet clear on this.
Try abs(result) > 0.0000001 instead of abs(result) >= 1.
Wow, that works! Thank you. So the gist is the result must be within 0.0000001 of 0?
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.
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.