{"record":{"id":"e931f1b4cec27257","repo":"TheAlgorithms/Python","slug":"could-not-find-root-in-given-interval","errorCode":null,"errorMessage":"could not find root in given interval.","messagePattern":"could not find root in given interval\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/numerical_analysis/bisection.py","lineNumber":32,"sourceCode":"    1.0\n    >>> bisection(lambda x: x ** 2 - 4 * x + 3, 2, 4)\n    3.0\n    >>> bisection(lambda x: x ** 2 - 4 * x + 3, 4, 1000)\n    Traceback (most recent call last):\n        ...\n    ValueError: could not find root in given interval.\n    \"\"\"\n    start: float = a\n    end: float = b\n    if function(a) == 0:  # one of the a or b is a root for the function\n        return a\n    elif function(b) == 0:\n        return b\n    elif (\n        function(a) * function(b) > 0\n    ):  # if none of these are root and they are both positive or negative,\n        # then this algorithm can't find the root\n        raise ValueError(\"could not find root in given interval.\")\n    else:\n        mid: float = start + (end - start) / 2.0\n        while abs(start - mid) > 10**-7:  # until precisely equals to 10^-7\n            if function(mid) == 0:\n                return mid\n            elif function(mid) * function(start) < 0:\n                end = mid\n            else:\n                start = mid\n            mid = start + (end - start) / 2.0\n        return mid\n\n\ndef f(x: float) -> float:\n    return x**3 - 2 * x - 5\n\n\nif __name__ == \"__main__\":","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/numerical_analysis/bisection.py#L14-L50","documentation":"Error \"could not find root in given interval.\" thrown in TheAlgorithms/Python.","triggerScenarios":"Thrown at maths/numerical_analysis/bisection.py:32 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","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."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}