TheAlgorithms/Python · error · ValueError

surface_area_cuboid() only accepts non-negative values

Error message

surface_area_cuboid() only accepts non-negative values

What it means

surface_area_cuboid() raises this ValueError when any of length, breadth, or height is negative. A cuboid's dimensions are physical lengths and cannot be negative, so the library checks each parameter before computing 2*(l*b + b*h + l*h). The first negative parameter triggers the raise; all three must be >= 0.

Source

Thrown at maths/area.py:55

    >>> surface_area_cuboid(0, 0, 0)
    0
    >>> surface_area_cuboid(1.6, 2.6, 3.6)
    38.56
    >>> surface_area_cuboid(-1, 2, 3)
    Traceback (most recent call last):
        ...
    ValueError: surface_area_cuboid() only accepts non-negative values
    >>> surface_area_cuboid(1, -2, 3)
    Traceback (most recent call last):
        ...
    ValueError: surface_area_cuboid() only accepts non-negative values
    >>> surface_area_cuboid(1, 2, -3)
    Traceback (most recent call last):
        ...
    ValueError: surface_area_cuboid() only accepts non-negative values
    """
    if length < 0 or breadth < 0 or height < 0:
        raise ValueError("surface_area_cuboid() only accepts non-negative values")
    return 2 * ((length * breadth) + (breadth * height) + (length * height))


def surface_area_sphere(radius: float) -> float:
    """
    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)

View on GitHub (pinned to f5988cc097)

Solutions

  1. Log which of length/breadth/height is negative before the call and fix the source of that value.
  2. If dimensions come from coordinates, ensure you compute abs(max - min) or sort the pair before subtracting.
  3. Replace sentinel values like -1 with None/0 at parse time.
  4. Catch ValueError at the boundary to report all three values in an application error.

Example fix

// before
area = surface_area_cuboid(l, b, h)  # one of them may be -1 (missing)

# after
if min(length, breadth, height) < 0:
    raise ValueError(f"cuboid dimensions must be >= 0: {length}, {breadth}, {height}")
area = surface_area_cuboid(length, breadth, height)
Defensive patterns

Strategy: validation

Validate before calling

if length < 0 or breadth < 0 or height < 0:
    raise ValueError(f'cuboid dimensions must be >= 0: {length}, {breadth}, {height}')
area = surface_area_cuboid(length, breadth, height)

Type guard

def is_valid_cuboid(dims: tuple) -> bool:
    return all(isinstance(d, (int, float)) and d >= 0 for d in dims)

Try / catch

try:
    area = surface_area_cuboid(l, b, h)
except ValueError as e:
    logger.error('cuboid input rejected: %s', (l, b, h))
    raise

Prevention

When it happens

Trigger: Calling surface_area_cuboid(length, breadth, height) with at least one argument < 0, e.g. surface_area_cuboid(-1, 2, 3) or surface_area_cuboid(1, -2, 3). Permutations of sign across the three parameters all trigger it.

Common situations: Dimensions read from a config file or database where a column stores -1 for unknown values; coordinate-derived box sizes (max - min) with swapped min/max; sensor data glitches producing negative readings.

Related errors


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