TheAlgorithms/Python · error · ValueError

vol_spheres_union() only accepts non-negative values, non-ze

Error message

vol_spheres_union() only accepts non-negative values, non-zero radius

What it means

Raised by vol_spheres_union(radius_1, radius_2, centers_distance) in maths/volume.py when either radius is <= 0 or the center distance is negative. Unlike vol_spheres_intersect, zero radii are rejected here because a union with a zero-radius sphere is degenerate and the guard is radius_1 <= 0 or radius_2 <= 0 or centers_distance < 0.

Source

Thrown at maths/volume.py:159

    45.814892864851146
    >>> vol_spheres_union(1.56, 2.2, 1.4)
    48.77802773671288
    >>> vol_spheres_union(0, 2, 1)
    Traceback (most recent call last):
        ...
    ValueError: vol_spheres_union() only accepts non-negative values, non-zero radius
    >>> vol_spheres_union('1.56', '2.2', '1.4')
    Traceback (most recent call last):
        ...
    TypeError: '<=' not supported between instances of 'str' and 'int'
    >>> vol_spheres_union(1, None, 1)
    Traceback (most recent call last):
        ...
    TypeError: '<=' not supported between instances of 'NoneType' and 'int'
    """

    if radius_1 <= 0 or radius_2 <= 0 or centers_distance < 0:
        raise ValueError(
            "vol_spheres_union() only accepts non-negative values, non-zero radius"
        )

    if centers_distance == 0:
        return vol_sphere(max(radius_1, radius_2))

    return (
        vol_sphere(radius_1)
        + vol_sphere(radius_2)
        - vol_spheres_intersect(radius_1, radius_2, centers_distance)
    )


def vol_cuboid(width: float, height: float, length: float) -> float:
    """
    Calculate the Volume of a Cuboid.

    :return: multiple of `width`, `length` and `height`

View on GitHub (pinned to f5988cc097)

Solutions

  1. Ensure both radii are strictly positive (filter or skip zero-radius entities) and the distance is >= 0 before calling.
  2. If a zero-radius sphere is possible in your data, handle it as the degenerate case yourself: the union is just vol_sphere(other_radius).
  3. Compute centers_distance with abs(x2 - x1) to eliminate negative distances.

Example fix

# before
vol = vol_spheres_union(r1, r2, dist)  # ValueError when r1 == 0

# after
from maths.volume import vol_sphere, vol_spheres_union
if r1 <= 0 and r2 <= 0:
    raise ValueError('both radii non-positive')
vol = vol_sphere(r2) if r1 <= 0 else (vol_sphere(r1) if r2 <= 0 else vol_spheres_union(r1, r2, dist))
Defensive patterns

Strategy: validation

Validate before calling

if radius_1 <= 0 or radius_2 <= 0 or centers_distance < 0:
    # handle degenerate case yourself or reject
    vol = vol_sphere(max(radius_1, radius_2)) if centers_distance == 0 else None
else:
    vol = vol_spheres_union(radius_1, radius_2, centers_distance)

Try / catch

try:
    vol = vol_spheres_union(r1, r2, d)
except ValueError as e:
    # zero/negative radii: skip or treat as single sphere
    if r1 <= 0 and r2 <= 0:
        raise
    vol = vol_sphere(max(r1, r2))

Prevention

When it happens

Trigger: Calling vol_spheres_union(0, 2, 1), vol_spheres_union(2, -2, 1) or vol_spheres_union(2, 2, -1); iterating over datasets where one entity legitimately has radius 0 (a point) and is fed straight into the union.

Common situations: Default-initializing radii to 0 and forgetting to overwrite before the call; mixing up this function's stricter zero-radius rule with vol_spheres_intersect's non-negative-only rule; signed center distances from coordinate subtraction.

Related errors


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