{"record":{"id":"1a6021658a34d195","repo":"TheAlgorithms/Python","slug":"vol-spheres-intersect-only-accepts-non-negative","errorCode":null,"errorMessage":"vol_spheres_intersect() only accepts non-negative values","messagePattern":"vol_spheres_intersect\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/volume.py","lineNumber":106,"sourceCode":"    >>> vol_spheres_intersect(2.6, 2.6, 1.6)\n    40.71504079052372\n    >>> vol_spheres_intersect(0, 0, 0)\n    0.0\n    >>> vol_spheres_intersect(-2, 2, 1)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_spheres_intersect() only accepts non-negative values\n    >>> vol_spheres_intersect(2, -2, 1)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_spheres_intersect() only accepts non-negative values\n    >>> vol_spheres_intersect(2, 2, -1)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_spheres_intersect() only accepts non-negative values\n    \"\"\"\n    if radius_1 < 0 or radius_2 < 0 or centers_distance < 0:\n        raise ValueError(\"vol_spheres_intersect() only accepts non-negative values\")\n    if centers_distance == 0:\n        return vol_sphere(min(radius_1, radius_2))\n\n    h1 = (\n        (radius_1 - radius_2 + centers_distance)\n        * (radius_1 + radius_2 - centers_distance)\n        / (2 * centers_distance)\n    )\n    h2 = (\n        (radius_2 - radius_1 + centers_distance)\n        * (radius_2 + radius_1 - centers_distance)\n        / (2 * centers_distance)\n    )\n\n    return vol_spherical_cap(h1, radius_2) + vol_spherical_cap(h2, radius_1)\n\n\ndef vol_spheres_union(","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/volume.py#L88-L124","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvol = vol_spheres_intersect(r1, r2, x2 - x1)  # ValueError when x2 < x1\n\n// after\nvol = vol_spheres_intersect(r1, r2, abs(x2 - x1))","handlingStrategy":"validation","validationCode":"def safe_sphere_intersect_args(r1: float, r2: float, d: float) -> bool:\n    return r1 >= 0 and r2 >= 0 and d >= 0\n\n# use: centers_distance = abs(x2 - x1) before the call","typeGuard":null,"tryCatchPattern":"try:\n    vol = vol_spheres_intersect(r1, r2, d)\nexcept ValueError as e:\n    raise ValueError(f'invalid sphere pair (r1={r1}, r2={r2}, d={d})') from e","preventionTips":["Always compute center distance as abs(coord2 - coord1).","Validate parsed numeric values for sign at the data boundary."],"tags":["math","validation","geometry","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}