TheAlgorithms/Python · error · ValueError

vol_prism() only accepts non-negative values

Error message

vol_prism() only accepts non-negative values

What it means

Raised by vol_prism(area_of_base, height) in maths/volume.py when height or area_of_base is negative. The function returns float(area_of_base * height) and validates both arguments first; zero inputs are valid and yield 0.0.

Source

Thrown at maths/volume.py:286

    >>> 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
    >>> vol_prism(-1, 1)
    Traceback (most recent call last):
        ...
    ValueError: vol_prism() only accepts non-negative values
    >>> vol_prism(1, -1)
    Traceback (most recent call last):
        ...
    ValueError: vol_prism() only accepts non-negative values
    """
    if height < 0 or area_of_base < 0:
        raise ValueError("vol_prism() only accepts non-negative values")
    return float(area_of_base * height)


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

    :return: :math:`\frac{1}{3} \cdot B \cdot h`

    >>> vol_pyramid(10, 3)
    10.0
    >>> vol_pyramid(1.5, 3)
    1.5
    >>> vol_pyramid(1.6, 1.6)
    0.8533333333333335
    >>> vol_pyramid(0, 0)
    0.0

View on GitHub (pinned to f5988cc097)

Solutions

  1. Normalize signed areas with abs() before the call.
  2. Treat downward extrusion explicitly as a magnitude (abs(depth)) and track direction in your own logic.
  3. Validate imported numeric columns for negatives before geometry processing.

Example fix

# before
vol = vol_prism(signed_area, depth)  # ValueError if depth < 0

# after
vol = vol_prism(abs(signed_area), abs(depth))
Defensive patterns

Strategy: validation

Validate before calling

area = abs(signed_area)
h = abs(height)
vol = vol_prism(area, h)

Try / catch

try:
    vol = vol_prism(area, h)
except ValueError as e:
    raise ValueError(f'invalid prism inputs: area={area}, h={h}') from e

Prevention

When it happens

Trigger: Calling vol_prism(-1, 1) or vol_prism(1, -1); feeding a signed polygon area from a winding-sensitive algorithm, or a negative height from inverted elevation data.

Common situations: Signed base areas from cross products that flip sign with vertex order; terrain/building models where 'downward' extrusion is encoded as negative height; unvalidated spreadsheet imports.

Related errors


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