TheAlgorithms/Python · error · ValueError
surface_area_cylinder() only accepts non-negative values
Error message
surface_area_cylinder() only accepts non-negative values
What it means
surface_area_cylinder() raises this ValueError when radius or height is negative. Both are physical dimensions enforced >= 0 before returning 2 * pi * r * (h + r). The guard exists because the formula would otherwise happily produce nonsense for negative inputs (or negative areas).
Source
Thrown at maths/area.py:201
>>> surface_area_cylinder(0, 0)
0.0
>>> surface_area_cylinder(6, 8)
527.7875658030853
>>> surface_area_cylinder(-1, -2)
Traceback (most recent call last):
...
ValueError: surface_area_cylinder() only accepts non-negative values
>>> surface_area_cylinder(1, -2)
Traceback (most recent call last):
...
ValueError: surface_area_cylinder() only accepts non-negative values
>>> surface_area_cylinder(-1, 2)
Traceback (most recent call last):
...
ValueError: surface_area_cylinder() only accepts non-negative values
"""
if radius < 0 or height < 0:
raise ValueError("surface_area_cylinder() only accepts non-negative values")
return 2 * pi * radius * (height + radius)
def surface_area_torus(torus_radius: float, tube_radius: float) -> float:
"""Calculate the Area of a Torus.
Wikipedia reference: https://en.wikipedia.org/wiki/Torus
:return 4pi^2 * torus_radius * tube_radius
>>> surface_area_torus(1, 1)
39.47841760435743
>>> surface_area_torus(4, 3)
473.7410112522892
>>> surface_area_torus(3, 4)
Traceback (most recent call last):
...
ValueError: surface_area_torus() does not support spindle or self intersecting tori
>>> surface_area_torus(1.6, 1.6)
101.06474906715503
>>> surface_area_torus(0, 0)
View on GitHub (pinned to f5988cc097)
Solutions
- Assert or check radius >= 0 and height >= 0 before calling; fix the negative source.
- For level-derived heights, clamp or reorder: height = abs(top - bottom).
- Add min=0 constraints in UI forms or schema validators (pydantic Field(ge=0)).
- Catch ValueError and re-raise with your application's error context.
Example fix
// before area = surface_area_cylinder(r, h) # h = level_a - level_b may be negative # after h = abs(level_a - level_b) area = surface_area_cylinder(r, h)
Defensive patterns
Strategy: validation
Validate before calling
if radius < 0 or height < 0:
raise ValueError(f'cylinder dimensions must be >= 0: radius={radius}, height={height}')
area = surface_area_cylinder(radius, height) Type guard
def is_valid_cylinder(r: float, h: float) -> bool:
return r >= 0 and h >= 0 Try / catch
try:
area = surface_area_cylinder(radius, height)
except ValueError as e:
raise ValueError(f'cylinder input rejected: {e}') from e Prevention
- For tank levels, compute height as abs(top - bottom) or clamp to 0.
- Enforce ge=0 in schema/pydantic models for physical dimensions.
When it happens
Trigger: Calling surface_area_cylinder(radius, height) with radius < 0 or height < 0, e.g. surface_area_cylinder(-1, 2) or surface_area_cylinder(1, -2).
Common situations: Tank/pipe sizing code where height is computed as outlet_level - inlet_level and can go negative when sensors report out of order; user form input not restricted to positive numbers; importing from spreadsheets with negative placeholders.
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/8f3491f15033ad49.
Report an issue: GitHub.