TheAlgorithms/Python · error · ValueError

vol_right_circ_cone() only accepts non-negative values

Error message

vol_right_circ_cone() only accepts non-negative values

What it means

Raised by vol_right_circ_cone(radius, height) in maths/volume.py when height or radius is negative. The function computes pi * radius**2 * height / 3.0 and guards both inputs; zero radius or height is fine and returns 0.0.

Source

Thrown at maths/volume.py:257

    :return: :math:`\frac{1}{3} \cdot \pi \cdot radius^2 \cdot height`

    >>> vol_right_circ_cone(2, 3)
    12.566370614359172
    >>> vol_right_circ_cone(0, 0)
    0.0
    >>> vol_right_circ_cone(1.6, 1.6)
    4.289321169701265
    >>> vol_right_circ_cone(-1, 1)
    Traceback (most recent call last):
        ...
    ValueError: vol_right_circ_cone() only accepts non-negative values
    >>> vol_right_circ_cone(1, -1)
    Traceback (most recent call last):
        ...
    ValueError: vol_right_circ_cone() only accepts non-negative values
    """
    if height < 0 or radius < 0:
        raise ValueError("vol_right_circ_cone() only accepts non-negative values")
    return pi * pow(radius, 2) * height / 3.0


def vol_prism(area_of_base: float, height: float) -> float:
    r"""
    | Calculate the Volume of a Prism.
    | Wikipedia reference: https://en.wikipedia.org/wiki/Prism_(geometry)

    :return: :math:`V = B \cdot h`

    >>> vol_prism(10, 2)
    20.0
    >>> vol_prism(11, 1)
    11.0
    >>> vol_prism(1.6, 1.6)
    2.5600000000000005
    >>> vol_prism(0, 0)
    0.0

View on GitHub (pinned to f5988cc097)

Solutions

  1. Clamp or reject negative radius/height at input parsing (float(value) then check >= 0).
  2. If the negative comes from an orientation convention, apply abs() or negate explicitly at the source.
  3. Catch ValueError around the call to produce a user-facing message when input comes from end users.

Example fix

# before
vol = vol_right_circ_cone(float(raw_radius), h)

# after
r = float(raw_radius)
if r < 0 or h < 0:
    raise ValueError(f'dimensions must be non-negative: r={r}, h={h}')
vol = vol_right_circ_cone(r, h)
Defensive patterns

Strategy: validation

Validate before calling

if radius < 0 or height < 0:
    raise ValueError(f'right circular cone needs non-negative r/h: {radius}, {height}')
vol = vol_right_circ_cone(radius, height)

Try / catch

try:
    vol = vol_right_circ_cone(r, h)
except ValueError:
    # input came from a user form: report instead of crashing
    return error_response('radius and height must be non-negative')

Prevention

When it happens

Trigger: Calling vol_right_circ_cone(-1, 1) or vol_right_circ_cone(1, -1); radii parsed from strings like '-2.5' or heights from inverted coordinate deltas.

Common situations: User-supplied dimensions in a CLI or web form containing a stray minus; measurement pipelines where a sensor sign convention produces negatives; reusing a signed 'drill depth' variable as height.

Related errors


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