TheAlgorithms/Python · error · ValueError

outer_radius must be greater than inner_radius

Error message

outer_radius must be greater than inner_radius

What it means

Raised by vol_hollow_circular_cylinder(inner_radius, outer_radius, height) in maths/volume.py when outer_radius <= inner_radius, i.e. the inner radius is not strictly smaller than the outer. A hollow cylinder requires a positive annulus, so equal radii (zero wall thickness, including the (0,0,0) case) are also rejected. It fires only after the non-negativity guard passes.

Source

Thrown at maths/volume.py:439

    Traceback (most recent call last):
        ...
    ValueError: vol_hollow_circular_cylinder() only accepts non-negative values
    >>> vol_hollow_circular_cylinder(2, 1, 3)
    Traceback (most recent call last):
        ...
    ValueError: outer_radius must be greater than inner_radius
    >>> vol_hollow_circular_cylinder(0, 0, 0)
    Traceback (most recent call last):
        ...
    ValueError: outer_radius must be greater than inner_radius
    """
    # Volume - (outer_radius squared - inner_radius squared) * pi * height
    if inner_radius < 0 or outer_radius < 0 or height < 0:
        raise ValueError(
            "vol_hollow_circular_cylinder() only accepts non-negative values"
        )
    if outer_radius <= inner_radius:
        raise ValueError("outer_radius must be greater than inner_radius")
    return pi * (pow(outer_radius, 2) - pow(inner_radius, 2)) * height


def vol_conical_frustum(height: float, radius_1: float, radius_2: float) -> float:
    """
    | Calculate the Volume of a Conical Frustum.
    | Wikipedia reference: https://en.wikipedia.org/wiki/Frustum

    >>> vol_conical_frustum(45, 7, 28)
    48490.482608158454
    >>> vol_conical_frustum(1, 1, 2)
    7.330382858376184
    >>> vol_conical_frustum(1.6, 2.6, 3.6)
    48.7240076620753
    >>> vol_conical_frustum(0, 0, 0)
    0.0
    >>> vol_conical_frustum(-2, 2, 1)
    Traceback (most recent call last):

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass (inner_radius, outer_radius, height) in that exact order and assert outer_radius > inner_radius > 0 before calling.
  2. If inner_radius is derived from wall thickness, validate outer_radius - wall_thickness > 0 (and wall_thickness > 0) first.
  3. Reject or special-case zero-thickness tubes in your data model instead of relying on the library error.

Example fix

# before
vol = vol_hollow_circular_cylinder(outer_r, inner_r, h)  # swapped -> ValueError

# after
assert 0 < inner_r < outer_r, f'need 0 < inner ({inner_r}) < outer ({outer_r})'
vol = vol_hollow_circular_cylinder(inner_r, outer_r, h)
Defensive patterns

Strategy: validation

Validate before calling

if not (0 <= inner_radius < outer_radius):
    raise ValueError(
        f'need inner < outer: inner={inner_radius}, outer={outer_radius}'
    )
vol = vol_hollow_circular_cylinder(inner_radius, outer_radius, height)

Try / catch

try:
    vol = vol_hollow_circular_cylinder(ir, oR, h)
except ValueError as e:
    if 'outer_radius' in str(e) and ir > oR:
        ir, oR = oR, ir  # tolerate swapped args if that is the known bug
        vol = vol_hollow_circular_cylinder(ir, oR, h)
    else:
        raise

Prevention

When it happens

Trigger: Calling vol_hollow_circular_cylinder(2, 1, 3) (swapped arguments), vol_hollow_circular_cylinder(3, 3, 5) (equal radii), or vol_hollow_circular_cylinder(0, 0, 0); computing inner_radius = outer_radius - wall_thickness with wall_thickness == 0 or negative.

Common situations: Argument order confusion (passing outer first); wall thickness of 0 from a default config; inner radius derived as outer minus thickness where the thickness data equals or exceeds the outer radius.

Related errors


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