TheAlgorithms/Python · error · ValueError
vol_hollow_circular_cylinder() only accepts non-negative val
Error message
vol_hollow_circular_cylinder() only accepts non-negative values
What it means
Raised by vol_hollow_circular_cylinder(inner_radius, outer_radius, height) in maths/volume.py when inner_radius, outer_radius or height is negative. This is the first of two guards: it only checks signs; a second guard then enforces outer_radius > inner_radius. The result is pi * (outer**2 - inner**2) * height.
Source
Thrown at maths/volume.py:435
Traceback (most recent call last):
...
ValueError: vol_hollow_circular_cylinder() only accepts non-negative values
>>> vol_hollow_circular_cylinder(1, 2, -3)
Traceback (most recent call last):
...
ValueError: vol_hollow_circular_cylinder() only accepts non-negative values
>>> vol_hollow_circular_cylinder(2, 1, 3)
Traceback (most recent call last):
...
ValueError: outer_radius must be greater than inner_radius
>>> vol_hollow_circular_cylinder(0, 0, 0)
Traceback (most recent call last):
...
ValueError: outer_radius must be greater than inner_radius
"""
# Volume - (outer_radius squared - inner_radius squared) * pi * height
if inner_radius < 0 or outer_radius < 0 or height < 0:
raise ValueError(
"vol_hollow_circular_cylinder() only accepts non-negative values"
)
if outer_radius <= inner_radius:
raise ValueError("outer_radius must be greater than inner_radius")
return pi * (pow(outer_radius, 2) - pow(inner_radius, 2)) * height
def vol_conical_frustum(height: float, radius_1: float, radius_2: float) -> float:
"""
| Calculate the Volume of a Conical Frustum.
| Wikipedia reference: https://en.wikipedia.org/wiki/Frustum
>>> vol_conical_frustum(45, 7, 28)
48490.482608158454
>>> vol_conical_frustum(1, 1, 2)
7.330382858376184
>>> vol_conical_frustum(1.6, 2.6, 3.6)
48.7240076620753View on GitHub (pinned to f5988cc097)
Solutions
- Validate all three arguments are >= 0 before the call (and outer > inner to also avoid the second ValueError).
- Recompute inner_radius = outer_radius - wall_thickness and clamp/validate the result when thickness data is offset-based.
- Check column mapping of your import file if negatives appear systematically.
Example fix
# before
vol = vol_hollow_circular_cylinder(outer_r - wall, outer_r, h) # negative when wall > outer_r
# after
inner_r = outer_r - wall
if inner_r < 0:
raise ValueError(f'wall thickness {wall} exceeds outer radius {outer_r}')
vol = vol_hollow_circular_cylinder(inner_r, outer_r, h) Defensive patterns
Strategy: validation
Validate before calling
if inner_radius < 0 or outer_radius < 0 or height < 0:
raise ValueError(f'hollow cylinder args must be >= 0: {inner_radius}, {outer_radius}, {height}')
vol = vol_hollow_circular_cylinder(inner_radius, outer_radius, height) Try / catch
try:
vol = vol_hollow_circular_cylinder(ir, oR, h)
except ValueError as e:
raise ValueError(f'bad tube geometry ir={ir}, oR={oR}, h={h}: {e}') from e Prevention
- Validate wall-thickness-derived inner radii before the call.
- Check import column mapping when negatives appear systematically.
When it happens
Trigger: Calling vol_hollow_circular_cylinder(-1, 2, 3), (1, -2, 3) or (1, 2, -3); pipe/tube parameters imported with sign errors or swapped-but-negative values.
Common situations: CAD or piping data where wall thickness is stored as a negative offset from the outer radius; measurement files with minus signs from a calibration offset; mixing inner/outer columns in a spreadsheet.
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/a20993463ced5934.
Report an issue: GitHub.