TheAlgorithms/Python · error · ValueError

surface_area_hemisphere() only accepts non-negative values

Error message

surface_area_hemisphere() only accepts non-negative values

What it means

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.

Source

Thrown at maths/area.py:102

    """
    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):
        ...
    ValueError: surface_area_hemisphere() only accepts non-negative values
    """
    if radius < 0:
        raise ValueError("surface_area_hemisphere() only accepts non-negative values")
    return 3 * pi * radius**2


def surface_area_cone(radius: float, height: float) -> float:
    """
    Calculate the Surface Area of a Cone.
    Wikipedia reference: https://en.wikipedia.org/wiki/Cone
    Formula: pi * r * (r + (h ** 2 + r ** 2) ** 0.5)

    >>> surface_area_cone(10, 24)
    1130.9733552923256
    >>> surface_area_cone(6, 8)
    301.59289474462014
    >>> surface_area_cone(1.6, 2.6)
    23.387862992395807
    >>> surface_area_cone(0, 0)
    0.0
    >>> surface_area_cone(-1, -2)

View on GitHub (pinned to f5988cc097)

Solutions

  1. Validate radius >= 0 at the source and correct the sign error.
  2. Use abs() only when the value is genuinely a magnitude with a sign artifact.
  3. Sanitize imported data: replace negative placeholders with 0 or drop the row.
  4. Catch ValueError and log the offending input for traceability.

Example fix

// before
area = surface_area_hemisphere(radius)  # radius from CSV, may be -1

# after
radius = next((float(r) for r in row if float(r) >= 0), None)
if radius is None:
    raise ValueError('no valid radius in row')
area = surface_area_hemisphere(radius)
Defensive patterns

Strategy: validation

Validate before calling

if radius < 0:
    raise ValueError(f'hemisphere radius must be >= 0, got {radius}')
area = surface_area_hemisphere(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_hemisphere(radius)
except ValueError as e:
    raise ValueError(f'invalid hemisphere radius {radius!r}: {e}') from e

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/9a66a684d77e66ce. Report an issue: GitHub.