TheAlgorithms/Python · error · ValueError
vol_spheres_intersect() only accepts non-negative values
Error message
vol_spheres_intersect() only accepts non-negative values
What it means
Raised by vol_spheres_intersect(radius_1, radius_2, centers_distance) in maths/volume.py when any of the three arguments is negative. The function computes the intersection volume of two spheres, which is only physically meaningful for non-negative radii and center distance, so it validates inputs up front and refuses to compute rather than returning a nonsense value. Note that centers_distance == 0 is allowed (returns the smaller sphere's volume).
Source
Thrown at maths/volume.py:106
>>> vol_spheres_intersect(2.6, 2.6, 1.6)
40.71504079052372
>>> vol_spheres_intersect(0, 0, 0)
0.0
>>> vol_spheres_intersect(-2, 2, 1)
Traceback (most recent call last):
...
ValueError: vol_spheres_intersect() only accepts non-negative values
>>> vol_spheres_intersect(2, -2, 1)
Traceback (most recent call last):
...
ValueError: vol_spheres_intersect() only accepts non-negative values
>>> vol_spheres_intersect(2, 2, -1)
Traceback (most recent call last):
...
ValueError: vol_spheres_intersect() only accepts non-negative values
"""
if radius_1 < 0 or radius_2 < 0 or centers_distance < 0:
raise ValueError("vol_spheres_intersect() only accepts non-negative values")
if centers_distance == 0:
return vol_sphere(min(radius_1, radius_2))
h1 = (
(radius_1 - radius_2 + centers_distance)
* (radius_1 + radius_2 - centers_distance)
/ (2 * centers_distance)
)
h2 = (
(radius_2 - radius_1 + centers_distance)
* (radius_2 + radius_1 - centers_distance)
/ (2 * centers_distance)
)
return vol_spherical_cap(h1, radius_2) + vol_spherical_cap(h2, radius_1)
def vol_spheres_union(View on GitHub (pinned to f5988cc097)
Solutions
- Check radius_1 >= 0, radius_2 >= 0 and centers_distance >= 0 before calling; wrap the distance computation in abs(), e.g. centers_distance = abs(x2 - x1).
- If values come from user input or files, coerce and validate at the boundary (float(value) plus a >= 0 check) before passing to geometry code.
- If a negative radius is genuinely a bug in your data, catch the ValueError to surface which value was bad instead of letting it propagate uncaught.
Example fix
// before vol = vol_spheres_intersect(r1, r2, x2 - x1) # ValueError when x2 < x1 // after vol = vol_spheres_intersect(r1, r2, abs(x2 - x1))
Defensive patterns
Strategy: validation
Validate before calling
def safe_sphere_intersect_args(r1: float, r2: float, d: float) -> bool:
return r1 >= 0 and r2 >= 0 and d >= 0
# use: centers_distance = abs(x2 - x1) before the call Try / catch
try:
vol = vol_spheres_intersect(r1, r2, d)
except ValueError as e:
raise ValueError(f'invalid sphere pair (r1={r1}, r2={r2}, d={d})') from e Prevention
- Always compute center distance as abs(coord2 - coord1).
- Validate parsed numeric values for sign at the data boundary.
When it happens
Trigger: Calling vol_spheres_intersect(-2, 2, 1), vol_spheres_intersect(2, -2, 1) or vol_spheres_intersect(2, 2, -1); also any call where a coordinate difference feeding centers_distance evaluates negative, e.g. vol_spheres_intersect(2, 2, a - b) when b > a.
Common situations: Computing center distance as abs(x1-x2) but forgetting abs() so a sign slips through; parsing radii from user input or a config file where a minus sign or negative unit conversion appears; passing raw measured values without sanitization.
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/1a6021658a34d195.
Report an issue: GitHub.