TheAlgorithms/Python · error · ValueError

area_reg_polygon() only accepts non-negative values as lengt

Error message

area_reg_polygon() only accepts non-negative values as length of a side

What it means

Raised by area_reg_polygon() when the side length passed to it is negative. The function computes the area of a regular polygon from its number of sides and side length; a negative length is geometrically meaningless, so it refuses it. Note the sides check runs first, so a bad sides value masks this error.

Source

Thrown at maths/area.py:550

three as number of sides
    >>> area_reg_polygon(5, -2)
    Traceback (most recent call last):
        ...
    ValueError: area_reg_polygon() only accepts non-negative values as \
length of a side
    >>> area_reg_polygon(-1, 2)
    Traceback (most recent call last):
        ...
    ValueError: area_reg_polygon() only accepts integers greater than or equal to \
three as number of sides
    """
    if not isinstance(sides, int) or sides < 3:
        raise ValueError(
            "area_reg_polygon() only accepts integers greater than or \
equal to three as number of sides"
        )
    elif length < 0:
        raise ValueError(
            "area_reg_polygon() only accepts non-negative values as \
length of a side"
        )
    return (sides * length**2) / (4 * tan(pi / sides))


if __name__ == "__main__":
    import doctest

    doctest.testmod(verbose=True)  # verbose so we can see methods missing tests

    print("[DEMO] Areas of various geometric shapes: \n")
    print(f"Rectangle: {area_rectangle(10, 20) = }")
    print(f"Square: {area_square(10) = }")
    print(f"Triangle: {area_triangle(10, 10) = }")
    print(f"Triangle: {area_triangle_three_sides(5, 12, 13) = }")
    print(f"Parallelogram: {area_parallelogram(10, 20) = }")
    print(f"Rhombus: {area_rhombus(10, 20) = }")

View on GitHub (pinned to f5988cc097)

Solutions

  1. Validate/abs or reject negative lengths before calling area_reg_polygon.
  2. Check the provenance of the length value — a negative usually indicates an upstream calculation bug.
  3. If zero-length sides are acceptable for your use, confirm they return 0 area rather than erroring (they do — only < 0 raises).

Example fix

// before
area = area_reg_polygon(sides, length)  # length may be -3

# after
if length < 0:
    raise ValueError(f"side length must be >= 0, got {length}")
area = area_reg_polygon(sides, length)
Defensive patterns

Strategy: validation

Validate before calling

if not isinstance(sides, int) or sides < 3:
    raise ValueError("sides must be an int >= 3")
if length < 0:
    raise ValueError(f"length must be non-negative, got {length}")

Type guard

def is_valid_polygon_input(sides: object, length: object) -> bool:
    return isinstance(sides, int) and sides >= 3 and isinstance(length, (int, float)) and length >= 0

Prevention

When it happens

Trigger: area_reg_polygon(6, -3) — any call where length < 0 while sides is an int >= 3, e.g. area_reg_polygon(5, -1.2).

Common situations: Feeding user input or measurements into the function without sanitizing sign; sign flips from upstream subtraction (e.g. length = a - b where b > a); CSV/parsed data containing negative values.

Related errors


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