TheAlgorithms/Python · error · ValueError

Depth cannot be less than 0

Error message

Depth cannot be less than 0

What it means

Raised by casimir_force() in physics/casimir_effect.py when the plate area argument is negative. Area enters the force/area/distance relations linearly, and a negative plate area is physically impossible, so it is rejected. It is the last of the three per-argument negativity checks.

Source

Thrown at backtracking/minimax.py:56

    >>> height = math.log(len(scores), 2)
    >>> minimax(0, 0, True, scores, height)
    65
    >>> minimax(-1, 0, True, scores, height)
    Traceback (most recent call last):
        ...
    ValueError: Depth cannot be less than 0
    >>> minimax(0, 0, True, [], 2)
    Traceback (most recent call last):
        ...
    ValueError: Scores cannot be empty
    >>> scores = [3, 5, 2, 9, 12, 5, 23, 23]
    >>> height = math.log(len(scores), 2)
    >>> minimax(0, 0, True, scores, height)
    12
    """

    if depth < 0:
        raise ValueError("Depth cannot be less than 0")
    if len(scores) == 0:
        raise ValueError("Scores cannot be empty")

    # Base case: If the current depth equals the height of the tree,
    # return the score of the current node.
    if depth == height:
        return scores[node_index]

    # If it's the maximizer's turn, choose the maximum score
    # between the two possible moves.
    if is_max:
        return max(
            minimax(depth + 1, node_index * 2, False, scores, height),
            minimax(depth + 1, node_index * 2 + 1, False, scores, height),
        )

    # If it's the minimizer's turn, choose the minimum score
    # between the two possible moves.

View on GitHub (pinned to f5988cc097)

Solutions

  1. Take abs() of the computed area if the sign is a geometric artifact.
  2. Validate area >= 0 before the call.
  3. Ensure exactly one argument is 0 and the others are positive reals.

Example fix

# before
casimir_force(force=2737e-21, area=signed_area, distance=0.0023746)

# after
casimir_force(force=2737e-21, area=abs(signed_area), distance=0.0023746)
Defensive patterns

Strategy: validation

Validate before calling

if area < 0:
    area = abs(area)  # geometric sign artifact
casimir_force(force=f, area=area, distance=d)

Try / catch

try:
    casimir_force(force=f, area=a, distance=d)
except ValueError as e:
    if "Area" in str(e):
        casimir_force(force=f, area=abs(a), distance=d)
    else:
        raise

Prevention

When it happens

Trigger: casimir_force(force=0, area=-0.0023, distance=0.0023746); passing an area computed as a signed determinant/cross-product result that went negative.

Common situations: Area computed geometrically (e.g. from vectors) where the sign depends on vertex ordering; passing -1 or other negative sentinels for 'unknown' fields.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/7a2cdba65997db73. Report an issue: GitHub.