TheAlgorithms/Python · error · ValueError
vol_torus() only accepts non-negative values
Error message
vol_torus() only accepts non-negative values
What it means
Raised by vol_torus(torus_radius, tube_radius) in maths/volume.py when either radius is negative. The function returns 2 * pi**2 * torus_radius * tube_radius**2; (0, 0) is valid and returns 0.0.
Source
Thrown at maths/volume.py:509
>>> vol_torus(4, 3)
710.6115168784338
>>> vol_torus(3, 4)
947.4820225045784
>>> vol_torus(1.6, 1.6)
80.85179925372404
>>> vol_torus(0, 0)
0.0
>>> vol_torus(-1, 1)
Traceback (most recent call last):
...
ValueError: vol_torus() only accepts non-negative values
>>> vol_torus(1, -1)
Traceback (most recent call last):
...
ValueError: vol_torus() only accepts non-negative values
"""
if torus_radius < 0 or tube_radius < 0:
raise ValueError("vol_torus() only accepts non-negative values")
return 2 * pow(pi, 2) * torus_radius * pow(tube_radius, 2)
def vol_icosahedron(tri_side: float) -> float:
"""
| Calculate the Volume of an Icosahedron.
| Wikipedia reference: https://en.wikipedia.org/wiki/Regular_icosahedron
>>> from math import isclose
>>> isclose(vol_icosahedron(2.5), 34.088984228514256)
True
>>> isclose(vol_icosahedron(10), 2181.694990624912374)
True
>>> isclose(vol_icosahedron(5), 272.711873828114047)
True
>>> isclose(vol_icosahedron(3.49), 92.740688412033628)
True
>>> vol_icosahedron(0)View on GitHub (pinned to f5988cc097)
Solutions
- Bound parameter sweeps to non-negative ranges before calling vol_torus.
- Validate both radii >= 0 at the point values enter your program (CLI args, config, mesh import).
- Skip or clamp iterations that would produce negative radii rather than calling the function.
Example fix
# before
for tube_r in numpy.arange(-2, 2, 0.1):
v = vol_torus(R, tube_r) # ValueError on negative tube_r
# after
for tube_r in numpy.arange(0, 2, 0.1):
v = vol_torus(R, tube_r) Defensive patterns
Strategy: validation
Validate before calling
if torus_radius < 0 or tube_radius < 0:
raise ValueError(f'torus radii must be >= 0: {torus_radius}, {tube_radius}')
vol = vol_torus(torus_radius, tube_radius) Try / catch
try:
v = vol_torus(R, r)
except ValueError:
continue # in sweeps: skip invalid parameter combos Prevention
- Constrain parameter-sweep ranges to non-negative values.
- Validate 3D model parameters at load time.
When it happens
Trigger: Calling vol_torus(-1, 1) or vol_torus(1, -1); torus parameters from CAD exports or parametric sweep code where one radius went negative during iteration.
Common situations: Optimization or parameter-sweep loops that step a radius through negative values; misordered arguments with a signed tube radius; unvalidated user parameters for 3D models.
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/8bee632012732635.
Report an issue: GitHub.