TheAlgorithms/Python · error · ValueError
area_ellipse() only accepts non-negative values
Error message
area_ellipse() only accepts non-negative values
What it means
area_ellipse() raises this ValueError when radius_x or radius_y is negative. Semi-axes are physical lengths; the library enforces >= 0 before returning pi * radius_x * radius_y. This also prevents the classic bug where two negatives multiply into a plausible-looking but wrong area.
Source
Thrown at maths/area.py:481
>>> area_ellipse(0, 0)
0.0
>>> area_ellipse(1.6, 2.6)
13.06902543893354
>>> area_ellipse(-10, 20)
Traceback (most recent call last):
...
ValueError: area_ellipse() only accepts non-negative values
>>> area_ellipse(10, -20)
Traceback (most recent call last):
...
ValueError: area_ellipse() only accepts non-negative values
>>> area_ellipse(-10, -20)
Traceback (most recent call last):
...
ValueError: area_ellipse() only accepts non-negative values
"""
if radius_x < 0 or radius_y < 0:
raise ValueError("area_ellipse() only accepts non-negative values")
return pi * radius_x * radius_y
def area_rhombus(diagonal_1: float, diagonal_2: float) -> float:
"""
Calculate the area of a rhombus.
>>> area_rhombus(10, 20)
100.0
>>> 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)
View on GitHub (pinned to f5988cc097)
Solutions
- Validate both radii >= 0 before calling; fix the negative source.
- Normalize signed extents to magnitudes with abs().
- Clean imported datasets before computation.
- Catch ValueError and report both values.
Example fix
// before area = area_ellipse(rx, ry) # rx = x_max - x_min style delta may be < 0 # after area = area_ellipse(abs(rx), abs(ry))
Defensive patterns
Strategy: validation
Validate before calling
if radius_x < 0 or radius_y < 0:
raise ValueError(f'ellipse semi-axes must be >= 0: {radius_x}, {radius_y}')
area = area_ellipse(radius_x, radius_y) Type guard
def is_valid_semi_axes(rx: float, ry: float) -> bool:
return rx >= 0 and ry >= 0 Try / catch
try:
area = area_ellipse(radius_x, radius_y)
except ValueError as e:
raise ValueError(f'invalid ellipse input: {e}') from e Prevention
- Convert signed extents to magnitudes with abs() before calling.
- Two negative semi-axes multiply to a positive area — the guard catches what your eye may not.
When it happens
Trigger: Calling area_ellipse(radius_x, radius_y) with either argument < 0, e.g. area_ellipse(10, -20) or area_ellipse(-10, -20).
Common situations: Semi-axes taken from orbital/elliptical data with signed conventions; coordinate-derived extents where a direction flips sign; imported data with negative 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/d2bbe614b1dde844.
Report an issue: GitHub.