TheAlgorithms/Python · error · ValueError

vol_spherical_cap() only accepts non-negative values

Error message

vol_spherical_cap() only accepts non-negative values

What it means

Raised by vol_spherical_cap(height, radius) in maths/volume.py when either argument is negative. The cap volume formula (1/3)*pi*height^2*(3*radius - height) is only physically valid for non-negative dimensions, and one shared message covers both parameters, so the exception alone does not tell you which one was bad.

Source

Thrown at maths/volume.py:55

    Calculate the volume of the spherical cap.

    >>> vol_spherical_cap(1, 2)
    5.235987755982988
    >>> vol_spherical_cap(1.6, 2.6)
    16.621119532592402
    >>> vol_spherical_cap(0, 0)
    0.0
    >>> vol_spherical_cap(-1, 2)
    Traceback (most recent call last):
        ...
    ValueError: vol_spherical_cap() only accepts non-negative values
    >>> vol_spherical_cap(1, -2)
    Traceback (most recent call last):
        ...
    ValueError: vol_spherical_cap() only accepts non-negative values
    """
    if height < 0 or radius < 0:
        raise ValueError("vol_spherical_cap() only accepts non-negative values")
    # Volume is 1/3 pi * height squared * (3 * radius - height)
    return 1 / 3 * pi * pow(height, 2) * (3 * radius - height)


def vol_spheres_intersect(
    radius_1: float, radius_2: float, centers_distance: float
) -> float:
    r"""
    Calculate the volume of the intersection of two spheres.

    The intersection is composed by two spherical caps and therefore its volume is the
    sum of the volumes of the spherical caps.
    First, it calculates the heights :math:`(h_1, h_2)` of the spherical caps,
    then the two volumes and it returns the sum.
    The height formulas are

    .. math::
        h_1 = \frac{(radius_1 - radius_2 + centers\_distance)

View on GitHub (pinned to f5988cc097)

Solutions

  1. Inspect both arguments when this fires — the message does not identify the culprit
  2. Guard differences used as heights: `height = max(0, top - bottom)` only if clamping is correct for your model, otherwise reject
  3. Validate all geometric dimensions as >= 0 at your input boundary

Example fix

// before
v = vol_spherical_cap(depth - fill, r)  # ValueError when fill > depth

// after
height = depth - fill
if height < 0:
    raise ValueError('fill exceeds depth')
v = vol_spherical_cap(height, r)
Defensive patterns

Strategy: validation

Validate before calling

if height < 0 or radius < 0:
    bad = 'height' if height < 0 else 'radius'
    raise ValueError(f'{bad} must be non-negative')
v = vol_spherical_cap(height, radius)

Prevention

When it happens

Trigger: Calling vol_spherical_cap(-1, 2) or vol_spherical_cap(1, -2) — either negative triggers it. Zero for either is fine (returns 0.0 when height is 0).

Common situations: Parsed dimensions with stray minus signs; height computed as a difference (e.g. depth - fill) going negative on edge cases, then passed unchecked.

Related errors


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