{"record":{"id":"f5446ff1ce8db457","repo":"TheAlgorithms/Python","slug":"vol-hemisphere-only-accepts-non-negative-values","errorCode":null,"errorMessage":"vol_hemisphere() only accepts non-negative values","messagePattern":"vol_hemisphere\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/volume.py","lineNumber":367,"sourceCode":"    | 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\n    >>> vol_hemisphere(0)\n    0.0\n    >>> vol_hemisphere(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_hemisphere() only accepts non-negative values\n    \"\"\"\n    if radius < 0:\n        raise ValueError(\"vol_hemisphere() only accepts non-negative values\")\n    # Volume is radius cubed * pi * 2/3\n    return pow(radius, 3) * pi * 2 / 3\n\n\ndef vol_circular_cylinder(radius: float, height: float) -> float:\n    r\"\"\"\n    | Calculate the Volume of a Circular Cylinder.\n    | Wikipedia reference: https://en.wikipedia.org/wiki/Cylinder\n\n    :return: :math:`\\pi \\cdot radius^2 \\cdot height`\n\n    >>> vol_circular_cylinder(1, 1)\n    3.141592653589793\n    >>> vol_circular_cylinder(4, 3)\n    150.79644737231007\n    >>> vol_circular_cylinder(1.6, 1.6)\n    12.867963509103795\n    >>> vol_circular_cylinder(0, 0)","sourceCodeStart":349,"sourceCodeEnd":385,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/volume.py#L349-L385","documentation":"Raised by vol_hemisphere(radius) in maths/volume.py when radius is negative. The function returns radius**3 * pi * 2 / 3 and guards its single argument; radius 0 is valid (0.0).","triggerScenarios":"Calling vol_hemisphere(-1) or vol_hemisphere(-0.2); dome/cutout radii from user input or derived computations that end up negative.","commonSituations":"Form inputs allowing '-2'; radius computed from a subtraction with swapped operands; mirroring geometry along an axis and accidentally keeping the sign.","solutions":["Validate radius >= 0 at the call site or input boundary.","Use abs() when the negative is purely a sign artifact of a directional computation.","Reject negative numeric fields during parsing with a message that identifies the source record."],"exampleFix":"# before\nv = vol_hemisphere(user_radius)\n\n# after\nr = abs(float(user_radius))\nv = vol_hemisphere(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_hemisphere(r)\nexcept ValueError as e:\n    raise ValueError(f'hemisphere radius must be >= 0, got {r}') from e","preventionTips":["Apply abs() to mirrored-geometry radii.","Reject negative numeric form fields during validation."],"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"}