TheAlgorithms/Python · error · ValueError

vol_conical_frustum() only accepts non-negative values

Error message

vol_conical_frustum() only accepts non-negative values

What it means

Raised by vol_conical_frustum(height, radius_1, radius_2) in maths/volume.py when any of height, radius_1 or radius_2 is negative. The formula is 1/3 * pi * height * (r1^2 + r2^2 + r1*r2), guarded by the sign check; note the argument order is (height, radius_1, radius_2), height first.

Source

Thrown at maths/volume.py:472

    >>> vol_conical_frustum(0, 0, 0)
    0.0
    >>> vol_conical_frustum(-2, 2, 1)
    Traceback (most recent call last):
        ...
    ValueError: vol_conical_frustum() only accepts non-negative values
    >>> vol_conical_frustum(2, -2, 1)
    Traceback (most recent call last):
        ...
    ValueError: vol_conical_frustum() only accepts non-negative values
    >>> vol_conical_frustum(2, 2, -1)
    Traceback (most recent call last):
        ...
    ValueError: vol_conical_frustum() only accepts non-negative values
    """
    # Volume is 1/3 * pi * height *
    #           (radius_1 squared + radius_2 squared + radius_1 * radius_2)
    if radius_1 < 0 or radius_2 < 0 or height < 0:
        raise ValueError("vol_conical_frustum() only accepts non-negative values")
    return (
        1
        / 3
        * pi
        * height
        * (pow(radius_1, 2) + pow(radius_2, 2) + radius_1 * radius_2)
    )


def vol_torus(torus_radius: float, tube_radius: float) -> float:
    r"""
    | Calculate the Volume of a Torus.
    | Wikipedia reference: https://en.wikipedia.org/wiki/Torus

    :return: :math:`2 \pi^2 \cdot torus\_radius \cdot tube\_radius^2`

    >>> vol_torus(1, 1)
    19.739208802178716

View on GitHub (pinned to f5988cc097)

Solutions

  1. Remember the signature is vol_conical_frustum(height, radius_1, radius_2) and validate all three are >= 0 before calling.
  2. Use keyword arguments (height=..., radius_1=..., radius_2=...) to make ordering mistakes impossible.
  3. Normalize signed heights to abs() at the source.

Example fix

# before
vol = vol_conical_frustum(r1, r2, h)  # wrong order: h lands in radius_2

# after
vol = vol_conical_frustum(height=h, radius_1=r1, radius_2=r2)
Defensive patterns

Strategy: validation

Validate before calling

if height < 0 or radius_1 < 0 or radius_2 < 0:
    raise ValueError(f'frustum args must be >= 0: h={height}, r1={radius_1}, r2={radius_2}')
vol = vol_conical_frustum(height, radius_1, radius_2)

Try / catch

try:
    vol = vol_conical_frustum(height, radius_1, radius_2)
except ValueError as e:
    raise ValueError(f'bad frustum inputs h={height}, r1={radius_1}, r2={radius_2}') from e

Prevention

When it happens

Trigger: Calling vol_conical_frustum(2, -2, 1) or vol_conical_frustum(2, 2, -1); accidentally passing (radius, radius, height) because the height-first signature differs from sibling functions, producing a negative only indirectly — but any negative argument triggers it.

Common situations: Argument-order confusion with other volume functions that take radii first; signed heights from inverted elevation data; negative radii from mis-parsed files.

Related errors


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