{"record":{"id":"3e8b40c70517d278","repo":"TheAlgorithms/Python","slug":"vol-sphere-only-accepts-non-negative-values","errorCode":null,"errorMessage":"vol_sphere() only accepts non-negative values","messagePattern":"vol_sphere\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/volume.py","lineNumber":340,"sourceCode":"    | Wikipedia reference: https://en.wikipedia.org/wiki/Sphere\n\n    :return: :math:`\\frac{4}{3} \\cdot \\pi \\cdot r^3`\n\n    >>> vol_sphere(5)\n    523.5987755982989\n    >>> vol_sphere(1)\n    4.1887902047863905\n    >>> vol_sphere(1.6)\n    17.15728467880506\n    >>> vol_sphere(0)\n    0.0\n    >>> vol_sphere(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_sphere() only accepts non-negative values\n    \"\"\"\n    if radius < 0:\n        raise ValueError(\"vol_sphere() only accepts non-negative values\")\n    # Volume is 4/3 * pi * radius cubed\n    return 4 / 3 * pi * pow(radius, 3)\n\n\ndef vol_hemisphere(radius: float) -> float:\n    r\"\"\"\n    | Calculate the volume of a hemisphere\n    | Wikipedia reference: https://en.wikipedia.org/wiki/Hemisphere\n    | Other references: https://www.cuemath.com/geometry/hemisphere\n\n    :return: :math:`\\frac{2}{3} \\cdot \\pi \\cdot radius^3`\n\n    >>> vol_hemisphere(1)\n    2.0943951023931953\n    >>> vol_hemisphere(7)\n    718.377520120866\n    >>> vol_hemisphere(1.6)\n    8.57864233940253","sourceCodeStart":322,"sourceCodeEnd":358,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/volume.py#L322-L358","documentation":"Raised by vol_sphere(radius) in maths/volume.py when radius is negative. The function returns 4/3 * pi * radius**3 and guards the single input; radius 0 is valid and returns 0.0. Note vol_sphere is also called internally by vol_spheres_intersect and vol_spheres_union, but those pre-validate, so in practice this error means you called vol_sphere directly with a negative value.","triggerScenarios":"Calling vol_sphere(-1) or vol_sphere(-0.5); radii parsed from strings with a leading '-', or derived from sqrt of mis-signed values, or from differences of unordered points.","commonSituations":"Parsing 'r: -3' rows from a data file; computing radius as x2 - x1 without abs(); sensor or CAD exports that encode direction in the radius sign.","solutions":["Check radius >= 0 before the call; use abs() when the value is known to be a magnitude with a possible sign artifact.","Validate numeric fields at parse time so negative radii fail with your own error identifying the offending row.","If negatives are meaningful in your domain (signed offsets), map them to magnitudes before calling vol_sphere."],"exampleFix":"# before\nv = vol_sphere(float(row['radius']))\n\n# after\nr = float(row['radius'])\nif r < 0:\n    raise ValueError(f\"negative radius in row {row}: {r}\")\nv = vol_sphere(r)","handlingStrategy":"type-guard","validationCode":"def is_valid_radius(r) -> bool:\n    return isinstance(r, (int, float)) and not isinstance(r, bool) and r >= 0","typeGuard":"def is_valid_radius(r) -> TypeGuard[float]:\n    return isinstance(r, (int, float)) and not isinstance(r, bool) and r >= 0","tryCatchPattern":"try:\n    v = vol_sphere(r)\nexcept ValueError:\n    r = abs(r)  # only if sign is known artifact; otherwise re-raise\n    v = vol_sphere(r)","preventionTips":["Radius is a magnitude: never let signed differences through un-absed.","Validate numeric parses at the data source with sign checks."],"tags":["math","validation","geometry","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}