{"record":{"id":"66de0599d8c15bf7","repo":"TheAlgorithms/Python","slug":"vol-spheres-union-only-accepts-non-negative-valu","errorCode":null,"errorMessage":"vol_spheres_union() only accepts non-negative values, non-zero radius","messagePattern":"vol_spheres_union\\(\\) only accepts non-negative values, non-zero radius","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/volume.py","lineNumber":159,"sourceCode":"    45.814892864851146\n    >>> vol_spheres_union(1.56, 2.2, 1.4)\n    48.77802773671288\n    >>> vol_spheres_union(0, 2, 1)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_spheres_union() only accepts non-negative values, non-zero radius\n    >>> vol_spheres_union('1.56', '2.2', '1.4')\n    Traceback (most recent call last):\n        ...\n    TypeError: '<=' not supported between instances of 'str' and 'int'\n    >>> vol_spheres_union(1, None, 1)\n    Traceback (most recent call last):\n        ...\n    TypeError: '<=' not supported between instances of 'NoneType' and 'int'\n    \"\"\"\n\n    if radius_1 <= 0 or radius_2 <= 0 or centers_distance < 0:\n        raise ValueError(\n            \"vol_spheres_union() only accepts non-negative values, non-zero radius\"\n        )\n\n    if centers_distance == 0:\n        return vol_sphere(max(radius_1, radius_2))\n\n    return (\n        vol_sphere(radius_1)\n        + vol_sphere(radius_2)\n        - vol_spheres_intersect(radius_1, radius_2, centers_distance)\n    )\n\n\ndef vol_cuboid(width: float, height: float, length: float) -> float:\n    \"\"\"\n    Calculate the Volume of a Cuboid.\n\n    :return: multiple of `width`, `length` and `height`","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/volume.py#L141-L177","documentation":"Raised by vol_spheres_union(radius_1, radius_2, centers_distance) in maths/volume.py when either radius is <= 0 or the center distance is negative. Unlike vol_spheres_intersect, zero radii are rejected here because a union with a zero-radius sphere is degenerate and the guard is radius_1 <= 0 or radius_2 <= 0 or centers_distance < 0.","triggerScenarios":"Calling vol_spheres_union(0, 2, 1), vol_spheres_union(2, -2, 1) or vol_spheres_union(2, 2, -1); iterating over datasets where one entity legitimately has radius 0 (a point) and is fed straight into the union.","commonSituations":"Default-initializing radii to 0 and forgetting to overwrite before the call; mixing up this function's stricter zero-radius rule with vol_spheres_intersect's non-negative-only rule; signed center distances from coordinate subtraction.","solutions":["Ensure both radii are strictly positive (filter or skip zero-radius entities) and the distance is >= 0 before calling.","If a zero-radius sphere is possible in your data, handle it as the degenerate case yourself: the union is just vol_sphere(other_radius).","Compute centers_distance with abs(x2 - x1) to eliminate negative distances."],"exampleFix":"# before\nvol = vol_spheres_union(r1, r2, dist)  # ValueError when r1 == 0\n\n# after\nfrom maths.volume import vol_sphere, vol_spheres_union\nif r1 <= 0 and r2 <= 0:\n    raise ValueError('both radii non-positive')\nvol = vol_sphere(r2) if r1 <= 0 else (vol_sphere(r1) if r2 <= 0 else vol_spheres_union(r1, r2, dist))","handlingStrategy":"validation","validationCode":"if radius_1 <= 0 or radius_2 <= 0 or centers_distance < 0:\n    # handle degenerate case yourself or reject\n    vol = vol_sphere(max(radius_1, radius_2)) if centers_distance == 0 else None\nelse:\n    vol = vol_spheres_union(radius_1, radius_2, centers_distance)","typeGuard":null,"tryCatchPattern":"try:\n    vol = vol_spheres_union(r1, r2, d)\nexcept ValueError as e:\n    # zero/negative radii: skip or treat as single sphere\n    if r1 <= 0 and r2 <= 0:\n        raise\n    vol = vol_sphere(max(r1, r2))","preventionTips":["This function rejects zero radii too, unlike vol_spheres_intersect.","Skip or special-case zero-radius entities before computing unions."],"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"}