TheAlgorithms/Python · error · ValueError

vol_cone() only accepts non-negative values

Error message

vol_cone() only accepts non-negative values

What it means

Raised by vol_cone(area_of_base, height) in maths/volume.py when height or area_of_base is negative. The formula area_of_base * height / 3.0 is only meaningful for non-negative base area and height, so both are guarded. Zero values are accepted and return 0.0.

Source

Thrown at maths/volume.py:230

    >>> vol_cone(10, 3)
    10.0
    >>> vol_cone(1, 1)
    0.3333333333333333
    >>> vol_cone(1.6, 1.6)
    0.8533333333333335
    >>> vol_cone(0, 0)
    0.0
    >>> vol_cone(-1, 1)
    Traceback (most recent call last):
        ...
    ValueError: vol_cone() only accepts non-negative values
    >>> vol_cone(1, -1)
    Traceback (most recent call last):
        ...
    ValueError: vol_cone() only accepts non-negative values
    """
    if height < 0 or area_of_base < 0:
        raise ValueError("vol_cone() only accepts non-negative values")
    return area_of_base * height / 3.0


def vol_right_circ_cone(radius: float, height: float) -> float:
    r"""
    | Calculate the Volume of a Right Circular Cone.
    | Wikipedia reference: https://en.wikipedia.org/wiki/Cone

    :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):

View on GitHub (pinned to f5988cc097)

Solutions

  1. Take abs() of signed areas from cross-product/shoelace computations before passing them in.
  2. Compute heights as a non-negative delta (abs(top - bottom) or sorted endpoints).
  3. Add an up-front assert/raise at your data layer so invalid negatives fail with your own message instead of the library's.

Example fix

# before
vol = vol_cone(shoelace_area(points), h)  # ValueError for clockwise winding

# after
vol = vol_cone(abs(shoelace_area(points)), abs(h))
Defensive patterns

Strategy: validation

Validate before calling

area = abs(area_of_base)
height = abs(height)
if area < 0 or height < 0:  # unreachable after abs(), kept for clarity
    raise ValueError
vol = vol_cone(area, height)

Try / catch

try:
    vol = vol_cone(area, height)
except ValueError as e:
    raise ValueError(f'bad cone inputs area={area}, height={height}') from e

Prevention

When it happens

Trigger: Calling vol_cone(-1, 1) or vol_cone(1, -1); passing a signed area computed from a cross product whose orientation flipped, or a height derived from a coordinate delta with inverted ordering.

Common situations: Signed area inputs from vector math (shoelace formula can yield negatives depending on vertex winding); height computed as top - bottom where top < bottom; raw measurement data with typos.

Related errors


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