TheAlgorithms/Python · error · ZeroDivisionError

float division by zero, could not find root

Error message

float division by zero, could not find root

What it means

Error "float division by zero, could not find root" thrown in TheAlgorithms/Python.

Source

Thrown at maths/numerical_analysis/intersection.py:34

    1.0000000000003888
    >>> intersection(lambda x: x ** 2 - 4 * x + 3, 0, 2)
    0.9999999998088019
    >>> intersection(lambda x: x ** 2 - 4 * x + 3, 2, 4)
    2.9999999998088023
    >>> intersection(lambda x: x ** 2 - 4 * x + 3, 4, 1000)
    3.0000000001786042
    >>> intersection(math.sin, -math.pi, math.pi)
    0.0
    >>> intersection(math.cos, -math.pi, math.pi)
    Traceback (most recent call last):
        ...
    ZeroDivisionError: float division by zero, could not find root
    """
    x_n: float = x0
    x_n1: float = x1
    while True:
        if x_n == x_n1 or function(x_n1) == function(x_n):
            raise ZeroDivisionError("float division by zero, could not find root")
        x_n2: float = x_n1 - (
            function(x_n1) / ((function(x_n1) - function(x_n)) / (x_n1 - x_n))
        )
        if abs(x_n2 - x_n1) < 10**-5:
            return x_n2
        x_n = x_n1
        x_n1 = x_n2


def f(x: float) -> float:
    """
    function is f(x) = x^3 - 2x - 5
    >>> f(2)
    -1.0
    """
    return math.pow(x, 3) - (2 * x) - 5

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pick initial points where the secant denominator (difference of function values) is non-zero.
  2. Choose a different starting interval or method if the function is flat near the guesses.

When it happens

Trigger: Thrown at maths/numerical_analysis/intersection.py:34 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/86ad301ad2006679. Report an issue: GitHub.