{"record":{"id":"1d63d8e72fb0f5b8","repo":"TheAlgorithms/Python","slug":"vol-spherical-cap-only-accepts-non-negative-valu","errorCode":null,"errorMessage":"vol_spherical_cap() only accepts non-negative values","messagePattern":"vol_spherical_cap\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/volume.py","lineNumber":55,"sourceCode":"    Calculate the volume of the spherical cap.\n\n    >>> vol_spherical_cap(1, 2)\n    5.235987755982988\n    >>> vol_spherical_cap(1.6, 2.6)\n    16.621119532592402\n    >>> vol_spherical_cap(0, 0)\n    0.0\n    >>> vol_spherical_cap(-1, 2)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_spherical_cap() only accepts non-negative values\n    >>> vol_spherical_cap(1, -2)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_spherical_cap() only accepts non-negative values\n    \"\"\"\n    if height < 0 or radius < 0:\n        raise ValueError(\"vol_spherical_cap() only accepts non-negative values\")\n    # Volume is 1/3 pi * height squared * (3 * radius - height)\n    return 1 / 3 * pi * pow(height, 2) * (3 * radius - height)\n\n\ndef vol_spheres_intersect(\n    radius_1: float, radius_2: float, centers_distance: float\n) -> float:\n    r\"\"\"\n    Calculate the volume of the intersection of two spheres.\n\n    The intersection is composed by two spherical caps and therefore its volume is the\n    sum of the volumes of the spherical caps.\n    First, it calculates the heights :math:`(h_1, h_2)` of the spherical caps,\n    then the two volumes and it returns the sum.\n    The height formulas are\n\n    .. math::\n        h_1 = \\frac{(radius_1 - radius_2 + centers\\_distance)","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/volume.py#L37-L73","documentation":"Raised by vol_spherical_cap(height, radius) in maths/volume.py when either argument is negative. The cap volume formula (1/3)*pi*height^2*(3*radius - height) is only physically valid for non-negative dimensions, and one shared message covers both parameters, so the exception alone does not tell you which one was bad.","triggerScenarios":"Calling vol_spherical_cap(-1, 2) or vol_spherical_cap(1, -2) — either negative triggers it. Zero for either is fine (returns 0.0 when height is 0).","commonSituations":"Parsed dimensions with stray minus signs; height computed as a difference (e.g. depth - fill) going negative on edge cases, then passed unchecked.","solutions":["Inspect both arguments when this fires — the message does not identify the culprit","Guard differences used as heights: `height = max(0, top - bottom)` only if clamping is correct for your model, otherwise reject","Validate all geometric dimensions as >= 0 at your input boundary"],"exampleFix":"// before\nv = vol_spherical_cap(depth - fill, r)  # ValueError when fill > depth\n\n// after\nheight = depth - fill\nif height < 0:\n    raise ValueError('fill exceeds depth')\nv = vol_spherical_cap(height, r)","handlingStrategy":"validation","validationCode":"if height < 0 or radius < 0:\n    bad = 'height' if height < 0 else 'radius'\n    raise ValueError(f'{bad} must be non-negative')\nv = vol_spherical_cap(height, radius)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["The message does not say which argument failed — check both yourself","Guard computed heights (differences like top - fill) against going negative","Validate all geometric dimensions >= 0 at your input boundary"],"tags":["math","geometry","valueerror","input-validation","volume"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}