TheAlgorithms/Python · error · ValueError
area_rhombus() only accepts non-negative values
Error message
area_rhombus() only accepts non-negative values
What it means
area_rhombus() raises this ValueError when diagonal_1 or diagonal_2 is negative. Diagonals are physical lengths; the library requires both >= 0 before returning 1/2 * d1 * d2. As with other product-based formulas, the guard prevents two negative inputs from silently yielding a positive, meaningless area.
Source
Thrown at maths/area.py:509
>>> area_rhombus(1.6, 2.6)
2.08
>>> area_rhombus(0, 0)
0.0
>>> area_rhombus(-1, -2)
Traceback (most recent call last):
...
ValueError: area_rhombus() only accepts non-negative values
>>> area_rhombus(1, -2)
Traceback (most recent call last):
...
ValueError: area_rhombus() only accepts non-negative values
>>> area_rhombus(-1, 2)
Traceback (most recent call last):
...
ValueError: area_rhombus() only accepts non-negative values
"""
if diagonal_1 < 0 or diagonal_2 < 0:
raise ValueError("area_rhombus() only accepts non-negative values")
return 1 / 2 * diagonal_1 * diagonal_2
def area_reg_polygon(sides: int, length: float) -> float:
"""
Calculate the area of a regular polygon.
Wikipedia reference: https://en.wikipedia.org/wiki/Polygon#Regular_polygons
Formula: (n*s^2*cot(pi/n))/4
>>> area_reg_polygon(3, 10)
43.301270189221945
>>> area_reg_polygon(4, 10)
100.00000000000001
>>> area_reg_polygon(0, 0)
Traceback (most recent call last):
...
ValueError: area_reg_polygon() only accepts integers greater than or equal to \
three as number of sides
View on GitHub (pinned to f5988cc097)
Solutions
- Check both diagonals >= 0 before the call; correct the sign at the data source.
- Convert signed spans to magnitudes with abs().
- Validate imported geometry data before processing.
- Catch ValueError for application-level error reporting.
Example fix
// before area = area_rhombus(d1, d2) # d1 = x2 - x1 may be negative # after area = area_rhombus(abs(x2 - x1), abs(y2 - y1))
Defensive patterns
Strategy: validation
Validate before calling
if diagonal_1 < 0 or diagonal_2 < 0:
raise ValueError(f'rhombus diagonals must be >= 0: {diagonal_1}, {diagonal_2}')
area = area_rhombus(diagonal_1, diagonal_2) Type guard
def is_valid_diagonals(d1: float, d2: float) -> bool:
return d1 >= 0 and d2 >= 0 Try / catch
try:
area = area_rhombus(diagonal_1, diagonal_2)
except ValueError as e:
raise ValueError(f'invalid rhombus input: {e}') from e Prevention
- Derive diagonals as abs(max - min) spans, not raw deltas.
- Validate CAD exports before geometric processing.
When it happens
Trigger: Calling area_rhombus(diagonal_1, diagonal_2) with either argument < 0, e.g. area_rhombus(-1, 2) or area_rhombus(1, -2).
Common situations: Diagonals measured as coordinate spans with signed direction; drafting/CAD exports with sign conventions; missing-data sentinels.
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/38c815f5566c3509.
Report an issue: GitHub.