TheAlgorithms/Python · error · ValueError
vol_circular_cylinder() only accepts non-negative values
Error message
vol_circular_cylinder() only accepts non-negative values
What it means
Raised by vol_circular_cylinder(radius, height) in maths/volume.py when height or radius is negative. The function returns radius**2 * height * pi after validating both inputs; zero radius or height is accepted and returns 0.0.
Source
Thrown at maths/volume.py:397
>>> vol_circular_cylinder(1, 1)
3.141592653589793
>>> vol_circular_cylinder(4, 3)
150.79644737231007
>>> vol_circular_cylinder(1.6, 1.6)
12.867963509103795
>>> vol_circular_cylinder(0, 0)
0.0
>>> vol_circular_cylinder(-1, 1)
Traceback (most recent call last):
...
ValueError: vol_circular_cylinder() only accepts non-negative values
>>> vol_circular_cylinder(1, -1)
Traceback (most recent call last):
...
ValueError: vol_circular_cylinder() only accepts non-negative values
"""
if height < 0 or radius < 0:
raise ValueError("vol_circular_cylinder() only accepts non-negative values")
# Volume is radius squared * height * pi
return pow(radius, 2) * height * pi
def vol_hollow_circular_cylinder(
inner_radius: float, outer_radius: float, height: float
) -> float:
"""
Calculate the Volume of a Hollow Circular Cylinder.
>>> vol_hollow_circular_cylinder(1, 2, 3)
28.274333882308138
>>> vol_hollow_circular_cylinder(1.6, 2.6, 3.6)
47.50088092227767
>>> vol_hollow_circular_cylinder(-1, 2, 3)
Traceback (most recent call last):
...
ValueError: vol_hollow_circular_cylinder() only accepts non-negative valuesView on GitHub (pinned to f5988cc097)
Solutions
- Convert signed depths to magnitudes with abs() before calling.
- Validate radius and height >= 0 where the data enters your program.
- Catch ValueError and re-raise with context (which object/record had the bad dimension) for easier debugging.
Example fix
# before vol = vol_circular_cylinder(r, borehole_depth) # ValueError if depth negative # after vol = vol_circular_cylinder(r, abs(borehole_depth))
Defensive patterns
Strategy: validation
Validate before calling
if radius < 0 or height < 0:
raise ValueError(f'cylinder needs non-negative r/h: {radius}, {height}')
vol = vol_circular_cylinder(radius, height) Try / catch
try:
vol = vol_circular_cylinder(r, h)
except ValueError:
vol = vol_circular_cylinder(abs(r), abs(h)) # only if signs are artifacts Prevention
- Downward depths stored as negatives must be converted with abs().
- Standardize a sign convention for depths across your dataset.
When it happens
Trigger: Calling vol_circular_cylinder(-1, 1) or vol_circular_cylinder(1, -1); heights encoded as signed depth (e.g. -10 for a borehole) passed directly.
Common situations: Downward measurements (drilling, excavation) stored as negative depth; CSV sign conventions differing between exports; reusing a signed delta as height.
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/826868a421ce5127.
Report an issue: GitHub.