TheAlgorithms/Python · error · ValueError

area_triangle() only accepts non-negative values

Error message

area_triangle() only accepts non-negative values

What it means

area_triangle() raises this ValueError when base or height is negative. Both are physical measurements used in (base * height) / 2; the library rejects negatives so a bad sign cannot corrupt the result. (In signed-area shoelace contexts you would use a different routine — this function is for plain geometric area.)

Source

Thrown at maths/area.py:311

    >>> area_triangle(1.6, 2.6)
    2.08
    >>> area_triangle(0, 0)
    0.0
    >>> area_triangle(-1, -2)
    Traceback (most recent call last):
        ...
    ValueError: area_triangle() only accepts non-negative values
    >>> area_triangle(1, -2)
    Traceback (most recent call last):
        ...
    ValueError: area_triangle() only accepts non-negative values
    >>> area_triangle(-1, 2)
    Traceback (most recent call last):
        ...
    ValueError: area_triangle() only accepts non-negative values
    """
    if base < 0 or height < 0:
        raise ValueError("area_triangle() only accepts non-negative values")
    return (base * height) / 2


def area_triangle_three_sides(side1: float, side2: float, side3: float) -> float:
    """
    Calculate area of triangle when the length of 3 sides are known.
    This function uses Heron's formula: https://en.wikipedia.org/wiki/Heron%27s_formula

    >>> area_triangle_three_sides(5, 12, 13)
    30.0
    >>> area_triangle_three_sides(10, 11, 12)
    51.521233486786784
    >>> area_triangle_three_sides(0, 0, 0)
    0.0
    >>> area_triangle_three_sides(1.6, 2.6, 3.6)
    1.8703742940919619
    >>> area_triangle_three_sides(-1, -2, -1)
    Traceback (most recent call last):

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass magnitudes: abs(base), abs(height) when computing unsigned geometric area from coordinates.
  2. Or use a coordinate-based formula (e.g. shoelace) instead of base*height/2 if signs matter.
  3. Validate inputs at ingestion to reject negatives.
  4. Catch ValueError and include the offending values in your error message.

Example fix

// before
area = area_triangle(x2 - x1, y2 - y1)  # deltas may be negative

# after
area = area_triangle(abs(x2 - x1), abs(y2 - y1))
Defensive patterns

Strategy: validation

Validate before calling

if base < 0 or height < 0:
    raise ValueError(f'triangle base/height must be >= 0: {base}, {height}')
area = area_triangle(base, height)

Type guard

def is_valid_triangle_dims(b: float, h: float) -> bool:
    return b >= 0 and h >= 0

Try / catch

try:
    area = area_triangle(base, height)
except ValueError as e:
    raise ValueError(f'invalid triangle input: {e}') from e

Prevention

When it happens

Trigger: Calling area_triangle(base, height) with base < 0 or height < 0, e.g. area_triangle(-1, 2) or area_triangle(1, -2).

Common situations: Using coordinate deltas as base/height where direction flips sign; porting shoelace-formula code that legitimately produces signed heights; measurement data with negative placeholders.

Related errors


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