{"record":{"id":"9a66a684d77e66ce","repo":"TheAlgorithms/Python","slug":"surface-area-hemisphere-only-accepts-non-negativ","errorCode":null,"errorMessage":"surface_area_hemisphere() only accepts non-negative values","messagePattern":"surface_area_hemisphere\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/area.py","lineNumber":102,"sourceCode":"    \"\"\"\r\n    Calculate the Surface Area of a Hemisphere.\r\n    Formula: 3 * pi * r^2\r\n\r\n    >>> surface_area_hemisphere(5)\r\n    235.61944901923448\r\n    >>> surface_area_hemisphere(1)\r\n    9.42477796076938\r\n    >>> surface_area_hemisphere(0)\r\n    0.0\r\n    >>> surface_area_hemisphere(1.1)\r\n    11.40398133253095\r\n    >>> surface_area_hemisphere(-1)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: surface_area_hemisphere() only accepts non-negative values\r\n    \"\"\"\r\n    if radius < 0:\r\n        raise ValueError(\"surface_area_hemisphere() only accepts non-negative values\")\r\n    return 3 * pi * radius**2\r\n\r\n\r\ndef surface_area_cone(radius: float, height: float) -> float:\r\n    \"\"\"\r\n    Calculate the Surface Area of a Cone.\r\n    Wikipedia reference: https://en.wikipedia.org/wiki/Cone\r\n    Formula: pi * r * (r + (h ** 2 + r ** 2) ** 0.5)\r\n\r\n    >>> surface_area_cone(10, 24)\r\n    1130.9733552923256\r\n    >>> surface_area_cone(6, 8)\r\n    301.59289474462014\r\n    >>> surface_area_cone(1.6, 2.6)\r\n    23.387862992395807\r\n    >>> surface_area_cone(0, 0)\r\n    0.0\r\n    >>> surface_area_cone(-1, -2)\r","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/area.py#L84-L120","documentation":"surface_area_hemisphere() raises this ValueError when radius is negative. The library requires radius >= 0 before applying the formula 3 * pi * radius**2. As with the sphere, squaring would mask a bad input, so the guard makes the domain violation explicit.","triggerScenarios":"Calling surface_area_hemisphere(radius) with radius < 0, e.g. surface_area_hemisphere(-1). Any pipeline that feeds an unvalidated signed number into the radius parameter triggers it.","commonSituations":"Reusing a signed delta variable as a radius; importing radii from spreadsheets where negative entries mean N/A; refactoring code that previously used raw r*r formulas without guards (behavior change on upgrade).","solutions":["Validate radius >= 0 at the source and correct the sign error.","Use abs() only when the value is genuinely a magnitude with a sign artifact.","Sanitize imported data: replace negative placeholders with 0 or drop the row.","Catch ValueError and log the offending input for traceability."],"exampleFix":"// before\narea = surface_area_hemisphere(radius)  # radius from CSV, may be -1\n\n# after\nradius = next((float(r) for r in row if float(r) >= 0), None)\nif radius is None:\n    raise ValueError('no valid radius in row')\narea = surface_area_hemisphere(radius)","handlingStrategy":"validation","validationCode":"if radius < 0:\n    raise ValueError(f'hemisphere radius must be >= 0, got {radius}')\narea = surface_area_hemisphere(radius)","typeGuard":"def is_valid_radius(r: object) -> bool:\n    return isinstance(r, (int, float)) and not isinstance(r, bool) and r >= 0","tryCatchPattern":"try:\n    area = surface_area_hemisphere(radius)\nexcept ValueError as e:\n    raise ValueError(f'invalid hemisphere radius {radius!r}: {e}') from e","preventionTips":["Sanitize imported radii (drop or fix negatives) before batch processing.","Remember squaring hides sign errors — never rely on r*r to fix input."],"tags":["python","math","geometry","validation","valueerror","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}