{"record":{"id":"0a88fee458f276c3","repo":"TheAlgorithms/Python","slug":"surface-area-sphere-only-accepts-non-negative-va","errorCode":null,"errorMessage":"surface_area_sphere() only accepts non-negative values","messagePattern":"surface_area_sphere\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/area.py","lineNumber":79,"sourceCode":"    Calculate the Surface Area of a Sphere.\r\n    Wikipedia reference: https://en.wikipedia.org/wiki/Sphere\r\n    Formula: 4 * pi * r^2\r\n\r\n    >>> surface_area_sphere(5)\r\n    314.1592653589793\r\n    >>> surface_area_sphere(1)\r\n    12.566370614359172\r\n    >>> surface_area_sphere(1.6)\r\n    32.169908772759484\r\n    >>> surface_area_sphere(0)\r\n    0.0\r\n    >>> surface_area_sphere(-1)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: surface_area_sphere() only accepts non-negative values\r\n    \"\"\"\r\n    if radius < 0:\r\n        raise ValueError(\"surface_area_sphere() only accepts non-negative values\")\r\n    return 4 * pi * radius**2\r\n\r\n\r\ndef surface_area_hemisphere(radius: float) -> float:\r\n    \"\"\"\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","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/area.py#L61-L97","documentation":"surface_area_sphere() raises this ValueError when radius is negative. Radius is a physical length; the library enforces radius >= 0 before returning 4 * pi * radius**2 rather than silently squaring the sign away. Note that squaring would hide the error, which is exactly why the guard exists.","triggerScenarios":"Calling surface_area_sphere(radius) with radius < 0, e.g. surface_area_sphere(-1). Common when radius is computed as a difference or parsed from user text including a stray '-' sign.","commonSituations":"Radius derived from coordinate deltas where the direction is negative; CLI arguments parsed with argparse allowing negative numbers; data files using negative values as null markers.","solutions":["Check radius >= 0 before calling and fix the upstream computation that produced a negative value.","Pass abs(radius) only if the sign truly is spurious and a magnitude is what you mean.","Restrict CLI argument types (e.g. custom argparse type that rejects negatives).","Wrap in try/except ValueError to surface a friendly message."],"exampleFix":"// before\narea = surface_area_sphere(r)  # r = center_x - point_x, can be negative\n\n# after\nr = abs(point_x - center_x)\narea = surface_area_sphere(r)","handlingStrategy":"validation","validationCode":"if radius < 0:\n    raise ValueError(f'sphere radius must be >= 0, got {radius}')\narea = surface_area_sphere(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_sphere(radius)\nexcept ValueError as e:\n    raise ValueError(f'cannot compute sphere area: {e}') from e","preventionTips":["Pass magnitudes (abs) for radii derived from deltas.","Use argparse type functions that reject negatives.","Treat the error as a data-quality signal, not noise to suppress."],"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"}