TheAlgorithms/Python · error · ValueError
surface_area_cone() only accepts non-negative values
Error message
surface_area_cone() only accepts non-negative values
What it means
surface_area_cone() raises this ValueError when radius or height is negative. Both are physical dimensions of the cone; the library checks each before computing pi * r * (r + sqrt(h^2 + r^2)). Without the guard, a negative radius would silently return a negative area.
Source
Thrown at maths/area.py:134
>>> surface_area_cone(1.6, 2.6)
23.387862992395807
>>> surface_area_cone(0, 0)
0.0
>>> surface_area_cone(-1, -2)
Traceback (most recent call last):
...
ValueError: surface_area_cone() only accepts non-negative values
>>> surface_area_cone(1, -2)
Traceback (most recent call last):
...
ValueError: surface_area_cone() only accepts non-negative values
>>> surface_area_cone(-1, 2)
Traceback (most recent call last):
...
ValueError: surface_area_cone() only accepts non-negative values
"""
if radius < 0 or height < 0:
raise ValueError("surface_area_cone() only accepts non-negative values")
return pi * radius * (radius + (height**2 + radius**2) ** 0.5)
def surface_area_conical_frustum(
radius_1: float, radius_2: float, height: float
) -> float:
"""
Calculate the Surface Area of a Conical Frustum.
>>> surface_area_conical_frustum(1, 2, 3)
45.511728065337266
>>> surface_area_conical_frustum(4, 5, 6)
300.7913575056268
>>> surface_area_conical_frustum(0, 0, 0)
0.0
>>> surface_area_conical_frustum(1.6, 2.6, 3.6)
78.57907060751548
>>> surface_area_conical_frustum(-1, 2, 3)
View on GitHub (pinned to f5988cc097)
Solutions
- Verify both radius and height are >= 0 immediately before the call and fix whichever is negative.
- If the values come from coordinates, normalize with abs(delta) before passing.
- Use keyword arguments surface_area_cone(radius=..., height=...) to avoid order mistakes.
- Catch ValueError to produce a domain-specific error message.
Example fix
// before area = surface_area_cone(r, h) # r or h may be negative deltas # after r, h = abs(r), abs(h) area = surface_area_cone(radius=r, height=h)
Defensive patterns
Strategy: validation
Validate before calling
if radius < 0 or height < 0:
raise ValueError(f'cone dimensions must be >= 0: radius={radius}, height={height}')
area = surface_area_cone(radius, height) Type guard
def is_valid_cone_dims(radius: float, height: float) -> bool:
return radius >= 0 and height >= 0 Try / catch
try:
area = surface_area_cone(radius, height)
except ValueError as e:
raise ValueError(f'cone input rejected ({radius=}, {height=}): {e}') from e Prevention
- Use keyword arguments (radius=, height=) to avoid positional mix-ups.
- Normalize coordinate-derived heights with abs().
When it happens
Trigger: Calling surface_area_cone(radius, height) with radius < 0 or height < 0, e.g. surface_area_cone(-1, 2) or surface_area_cone(1, -2). Argument order confusion (passing height where radius belongs) with signed values also triggers it.
Common situations: Mixing up positional arguments when refactoring to keyword style; heights computed as top - bottom with reversed coordinates; measurement imports containing 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_conical_frustum() only accepts non-negative val
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/2d5124d582f9732d.
Report an issue: GitHub.