TheAlgorithms/Python · error · ValueError

surface_area_conical_frustum() only accepts non-negative val

Error message

surface_area_conical_frustum() only accepts non-negative values

What it means

surface_area_conical_frustum() raises this ValueError when radius_1, radius_2, or height is negative. All three are physical dimensions; the library validates them before computing the frustum surface via slant height sqrt(h^2 + (r1 - r2)^2). Note that r1 < r2 is allowed (the difference is squared), only negativity is rejected.

Source

Thrown at maths/area.py:166

    >>> surface_area_conical_frustum(0, 0, 0)
    0.0
    >>> surface_area_conical_frustum(1.6, 2.6, 3.6)
    78.57907060751548
    >>> surface_area_conical_frustum(-1, 2, 3)
    Traceback (most recent call last):
        ...
    ValueError: surface_area_conical_frustum() only accepts non-negative values
    >>> surface_area_conical_frustum(1, -2, 3)
    Traceback (most recent call last):
        ...
    ValueError: surface_area_conical_frustum() only accepts non-negative values
    >>> surface_area_conical_frustum(1, 2, -3)
    Traceback (most recent call last):
        ...
    ValueError: surface_area_conical_frustum() only accepts non-negative values
    """
    if radius_1 < 0 or radius_2 < 0 or height < 0:
        raise ValueError(
            "surface_area_conical_frustum() only accepts non-negative values"
        )
    slant_height = (height**2 + (radius_1 - radius_2) ** 2) ** 0.5
    return pi * ((slant_height * (radius_1 + radius_2)) + radius_1**2 + radius_2**2)


def surface_area_cylinder(radius: float, height: float) -> float:
    """
    Calculate the Surface Area of a Cylinder.
    Wikipedia reference: https://en.wikipedia.org/wiki/Cylinder
    Formula: 2 * pi * r * (h + r)

    >>> surface_area_cylinder(7, 10)
    747.6990515543707
    >>> surface_area_cylinder(1.6, 2.6)
    42.22300526424682
    >>> surface_area_cylinder(0, 0)
    0.0

View on GitHub (pinned to f5988cc097)

Solutions

  1. Check all three arguments with min(radius_1, radius_2, height) >= 0 before the call.
  2. Fix the upstream source of the negative dimension (data entry, subtraction direction).
  3. Treat the radii as magnitudes: pass abs() only if sign is meaningless in your data model.
  4. Catch ValueError and report which dimension was invalid.

Example fix

// before
area = surface_area_conical_frustum(r1, r2, h)

# after
if r1 < 0 or r2 < 0 or h < 0:
    raise ValueError(f'frustum dimensions must be >= 0: r1={r1}, r2={r2}, h={h}')
area = surface_area_conical_frustum(r1, r2, h)
Defensive patterns

Strategy: validation

Validate before calling

if radius_1 < 0 or radius_2 < 0 or height < 0:
    raise ValueError(f'frustum dimensions must be >= 0: {radius_1}, {radius_2}, {height}')
area = surface_area_conical_frustum(radius_1, radius_2, height)

Type guard

def is_valid_frustum(r1: float, r2: float, h: float) -> bool:
    return min(r1, r2, h) >= 0

Try / catch

try:
    area = surface_area_conical_frustum(r1, r2, h)
except ValueError as e:
    raise ValueError(f'frustum input rejected: {e}') from e

Prevention

When it happens

Trigger: Calling surface_area_conical_frustum(radius_1, radius_2, height) with any argument < 0, e.g. (-1, 2, 3), (1, -2, 3), or (1, 2, -3). Swapping argument order never triggers it by itself — only negative values do.

Common situations: Radii taken from an ordered list where an entry is a negative placeholder; heights derived from level differences (bottom - top); migrating from a hand-written formula that had no guards.

Related errors


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