TheAlgorithms/Python · error · ValueError
surface_area_sphere() only accepts non-negative values
Error message
surface_area_sphere() only accepts non-negative values
What it means
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.
Source
Thrown at maths/area.py:79
Calculate the Surface Area of a Sphere.
Wikipedia reference: https://en.wikipedia.org/wiki/Sphere
Formula: 4 * pi * r^2
>>> surface_area_sphere(5)
314.1592653589793
>>> surface_area_sphere(1)
12.566370614359172
>>> surface_area_sphere(1.6)
32.169908772759484
>>> surface_area_sphere(0)
0.0
>>> surface_area_sphere(-1)
Traceback (most recent call last):
...
ValueError: surface_area_sphere() only accepts non-negative values
"""
if radius < 0:
raise ValueError("surface_area_sphere() only accepts non-negative values")
return 4 * pi * radius**2
def surface_area_hemisphere(radius: float) -> float:
"""
Calculate the Surface Area of a Hemisphere.
Formula: 3 * pi * r^2
>>> surface_area_hemisphere(5)
235.61944901923448
>>> surface_area_hemisphere(1)
9.42477796076938
>>> surface_area_hemisphere(0)
0.0
>>> surface_area_hemisphere(1.1)
11.40398133253095
>>> surface_area_hemisphere(-1)
Traceback (most recent call last):
View on GitHub (pinned to f5988cc097)
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.
Example fix
// before area = surface_area_sphere(r) # r = center_x - point_x, can be negative # after r = abs(point_x - center_x) area = surface_area_sphere(r)
Defensive patterns
Strategy: validation
Validate before calling
if radius < 0:
raise ValueError(f'sphere radius must be >= 0, got {radius}')
area = surface_area_sphere(radius) Type guard
def is_valid_radius(r: object) -> bool:
return isinstance(r, (int, float)) and not isinstance(r, bool) and r >= 0 Try / catch
try:
area = surface_area_sphere(radius)
except ValueError as e:
raise ValueError(f'cannot compute sphere area: {e}') from e Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- surface_area_cube() only accepts non-negative values
- surface_area_cuboid() only accepts non-negative values
- surface_area_hemisphere() only accepts non-negative values
- surface_area_cone() only accepts non-negative values
- surface_area_conical_frustum() only accepts non-negative val
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/0a88fee458f276c3.
Report an issue: GitHub.