TheAlgorithms/Python · error · ValueError

surface_area_torus() only accepts non-negative values

Error message

surface_area_torus() only accepts non-negative values

What it means

surface_area_torus() raises this ValueError when torus_radius or tube_radius is negative. The library enforces non-negative radii before computing 4 * pi^2 * R * r. This is the first of two guards in the function — even after passing it, a second check rejects spindle/self-intersecting geometry (torus_radius < tube_radius).

Source

Thrown at maths/area.py:231

    >>> surface_area_torus(3, 4)
    Traceback (most recent call last):
        ...
    ValueError: surface_area_torus() does not support spindle or self intersecting tori
    >>> surface_area_torus(1.6, 1.6)
    101.06474906715503
    >>> surface_area_torus(0, 0)
    0.0
    >>> surface_area_torus(-1, 1)
    Traceback (most recent call last):
        ...
    ValueError: surface_area_torus() only accepts non-negative values
    >>> surface_area_torus(1, -1)
    Traceback (most recent call last):
        ...
    ValueError: surface_area_torus() only accepts non-negative values
    """
    if torus_radius < 0 or tube_radius < 0:
        raise ValueError("surface_area_torus() only accepts non-negative values")
    if torus_radius < tube_radius:
        raise ValueError(
            "surface_area_torus() does not support spindle or self intersecting tori"
        )
    return 4 * pow(pi, 2) * torus_radius * tube_radius


def area_rectangle(length: float, width: float) -> float:
    """
    Calculate the area of a rectangle.

    >>> area_rectangle(10, 20)
    200
    >>> area_rectangle(1.6, 2.6)
    4.16
    >>> area_rectangle(0, 0)
    0
    >>> area_rectangle(-1, -2)

View on GitHub (pinned to f5988cc097)

Solutions

  1. Clamp or validate both radii to >= 0 before the call.
  2. If parameters come from optimization loops, bound the search domain to [0, inf).
  3. Fix the sign at the data source rather than suppressing the error.
  4. Catch ValueError to emit a clear message naming both radii.

Example fix

// before
area = surface_area_torus(R, r)  # R or r may go negative in a sweep

# after
R = max(R, 0.0)
r = max(r, 0.0)
area = surface_area_torus(R, r)
Defensive patterns

Strategy: validation

Validate before calling

if torus_radius < 0 or tube_radius < 0:
    raise ValueError(f'torus radii must be >= 0: R={torus_radius}, r={tube_radius}')
area = surface_area_torus(torus_radius, tube_radius)

Type guard

def is_valid_torus_radii(R: float, r: float) -> bool:
    return R >= 0 and r >= 0

Try / catch

try:
    area = surface_area_torus(R, r)
except ValueError as e:
    raise ValueError(f'torus input rejected (R={R}, r={r}): {e}') from e

Prevention

When it happens

Trigger: Calling surface_area_torus(torus_radius, tube_radius) with either argument < 0, e.g. surface_area_torus(-1, 1) or surface_area_torus(1, -1).

Common situations: CAD/game asset generation with parametric sweeps where a radius parameter goes negative at range extremes; sliders in a UI bound to radius values that can cross zero; parsing dims from JSON with sign errors.

Related errors


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