TheAlgorithms/Python · error · ValueError
area_rectangle() only accepts non-negative values
Error message
area_rectangle() only accepts non-negative values
What it means
area_rectangle() raises this ValueError when length or width is negative. The library enforces non-negative side lengths before returning length * width. Although a negative times a negative would give a positive number, that result would be meaningless as an area, so the input is rejected up front.
Source
Thrown at maths/area.py:263
>>> area_rectangle(1.6, 2.6)
4.16
>>> area_rectangle(0, 0)
0
>>> area_rectangle(-1, -2)
Traceback (most recent call last):
...
ValueError: area_rectangle() only accepts non-negative values
>>> area_rectangle(1, -2)
Traceback (most recent call last):
...
ValueError: area_rectangle() only accepts non-negative values
>>> area_rectangle(-1, 2)
Traceback (most recent call last):
...
ValueError: area_rectangle() only accepts non-negative values
"""
if length < 0 or width < 0:
raise ValueError("area_rectangle() only accepts non-negative values")
return length * width
def area_square(side_length: float) -> float:
"""
Calculate the area of a square.
>>> area_square(10)
100
>>> area_square(0)
0
>>> area_square(1.6)
2.5600000000000005
>>> area_square(-1)
Traceback (most recent call last):
...
ValueError: area_square() only accepts non-negative values
"""
View on GitHub (pinned to f5988cc097)
Solutions
- Validate length >= 0 and width >= 0 before the call; correct the sign at the source.
- Convert signed measurements to magnitudes with abs() when direction is irrelevant.
- Replace -1 sentinels with explicit missing-value handling during import.
- Catch ValueError for user-facing reporting.
Example fix
// before area = area_rectangle(l, w) # l = x2 - x1 may be negative # after l, w = abs(x2 - x1), abs(y2 - y1) area = area_rectangle(l, w)
Defensive patterns
Strategy: validation
Validate before calling
if length < 0 or width < 0:
raise ValueError(f'rectangle sides must be >= 0: {length}, {width}')
area = area_rectangle(length, width) Type guard
def is_valid_rect_sides(l: float, w: float) -> bool:
return l >= 0 and w >= 0 Try / catch
try:
area = area_rectangle(length, width)
except ValueError as e:
raise ValueError(f'invalid rectangle input: {e}') from e Prevention
- Convert signed surveying deltas to magnitudes before use.
- Replace -1 sentinels with None at import time.
When it happens
Trigger: Calling area_rectangle(length, width) with length < 0 or width < 0, e.g. area_rectangle(-1, 2) or area_rectangle(1, -2).
Common situations: Room/plot dimensions from survey data with sign conventions (e.g. southward as negative); spreadsheet -1 sentinels for missing widths; differences of coordinates used directly as lengths.
Related errors
- surface_area_cube() only accepts non-negative values
- surface_area_cuboid() only accepts non-negative values
- surface_area_sphere() only accepts non-negative values
- surface_area_hemisphere() only accepts non-negative values
- surface_area_cone() only accepts non-negative values
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/42c7dfe9302da1e0.
Report an issue: GitHub.