TheAlgorithms/Python · error · ValueError
could not find root in given interval.
Error message
could not find root in given interval.
What it means
Error "could not find root in given interval." thrown in TheAlgorithms/Python.
Source
Thrown at maths/numerical_analysis/bisection.py:32
1.0
>>> bisection(lambda x: x ** 2 - 4 * x + 3, 2, 4)
3.0
>>> bisection(lambda x: x ** 2 - 4 * x + 3, 4, 1000)
Traceback (most recent call last):
...
ValueError: could not find root in given interval.
"""
start: float = a
end: float = b
if function(a) == 0: # one of the a or b is a root for the function
return a
elif function(b) == 0:
return b
elif (
function(a) * function(b) > 0
): # if none of these are root and they are both positive or negative,
# then this algorithm can't find the root
raise ValueError("could not find root in given interval.")
else:
mid: float = start + (end - start) / 2.0
while abs(start - mid) > 10**-7: # until precisely equals to 10^-7
if function(mid) == 0:
return mid
elif function(mid) * function(start) < 0:
end = mid
else:
start = mid
mid = start + (end - start) / 2.0
return mid
def f(x: float) -> float:
return x**3 - 2 * x - 5
if __name__ == "__main__":View on GitHub (pinned to f5988cc097)
Solutions
- Choose an interval [a, b] where f(a) and f(b) have opposite signs.
- Widen the search interval or plot the function to locate a sign change.
When it happens
Trigger: Thrown at maths/numerical_analysis/bisection.py:32 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/e931f1b4cec27257.
Report an issue: GitHub.