Suppose i have a quadratic equation like this, 2x^2 - 4x - 5 = 0
, the solution here is x1=2.87 and x2=-0.87. I tried this python snippet to find the non-negative solution(2.87) by setting range 0 to 1000 and it worked but how to find the negative one too?. I tried the range -1000 to 0, but no luck!
def solve():
low, high=0,1000
while (high-low)>10e-5:
x = (low+high)/2
fx = 2*(x**2)-4*x-5
if fx>0:
high=x
else:
low=x
return low
print(solve())
Or I am doing this whole thing wrong? What is the strategy to work with negative ranges and floating numbers in binary search?