{"record":{"id":"7f9118b3b30b363d","repo":"TheAlgorithms/Python","slug":"surface-area-cube-only-accepts-non-negative-valu","errorCode":null,"errorMessage":"surface_area_cube() only accepts non-negative values","messagePattern":"surface_area_cube\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/area.py","lineNumber":27,"sourceCode":"def surface_area_cube(side_length: float) -> float:\r\n    \"\"\"\r\n    Calculate the Surface Area of a Cube.\r\n\r\n    >>> surface_area_cube(1)\r\n    6\r\n    >>> surface_area_cube(1.6)\r\n    15.360000000000003\r\n    >>> surface_area_cube(0)\r\n    0\r\n    >>> surface_area_cube(3)\r\n    54\r\n    >>> surface_area_cube(-1)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: surface_area_cube() only accepts non-negative values\r\n    \"\"\"\r\n    if side_length < 0:\r\n        raise ValueError(\"surface_area_cube() only accepts non-negative values\")\r\n    return 6 * side_length**2\r\n\r\n\r\ndef surface_area_cuboid(length: float, breadth: float, height: float) -> float:\r\n    \"\"\"\r\n    Calculate the Surface Area of a Cuboid.\r\n\r\n    >>> surface_area_cuboid(1, 2, 3)\r\n    22\r\n    >>> surface_area_cuboid(0, 0, 0)\r\n    0\r\n    >>> surface_area_cuboid(1.6, 2.6, 3.6)\r\n    38.56\r\n    >>> surface_area_cuboid(-1, 2, 3)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: surface_area_cuboid() only accepts non-negative values\r\n    >>> surface_area_cuboid(1, -2, 3)\r","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/area.py#L9-L45","documentation":"surface_area_cube() raises this ValueError when side_length is negative, because a cube cannot have a negative edge length. The library validates domain constraints before computing 6 * side_length**2 and refuses to silently return a mathematically meaningless result. It is a deliberate input-domain guard, not a bug. Zero and positive floats/ints are accepted.","triggerScenarios":"Calling surface_area_cube(side_length) with side_length < 0, e.g. surface_area_cube(-1). Typically happens when the value comes from a subtraction like (a - b) that can go negative, or from unvalidated user/CSV input.","commonSituations":"Parsing measurements that use a minus sign as a placeholder for missing data; computing edge length as a difference of coordinates that flips sign; unit-conversion mistakes that negate the value.","solutions":["Inspect the value passed as side_length right before the call and fix the upstream sign error (e.g. swap the operands of the subtraction that produced it).","If a magnitude is intended, pass abs(side_length) or the correct positive measurement.","Validate numeric input at ingestion (reject or sanitize negatives) so the guard never fires.","Wrap the call in try/except ValueError only to convert it into a user-facing message."],"exampleFix":"// before\narea = surface_area_cube(edge)  # edge == -3 from bad input\n\n# after\nif edge < 0:\n    raise ValueError(f\"edge length must be >= 0, got {edge}\")\narea = surface_area_cube(edge)","handlingStrategy":"validation","validationCode":"if side_length < 0:\n    raise ValueError(f'cube side must be >= 0, got {side_length}')\narea = surface_area_cube(side_length)","typeGuard":"def is_valid_cube_side(x: object) -> bool:\n    return isinstance(x, (int, float)) and not isinstance(x, bool) and x >= 0","tryCatchPattern":"try:\n    area = surface_area_cube(side_length)\nexcept ValueError as e:\n    raise ValueError(f'invalid cube input {side_length!r}: {e}') from e","preventionTips":["Validate dimensions at data ingestion, not at the math call site.","Never use negative numbers as missing-value sentinels; use None.","Compute lengths as abs(delta) when derived from coordinates."],"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"}