{"record":{"id":"9434b67737def5ad","repo":"TheAlgorithms/Python","slug":"surface-area-cuboid-only-accepts-non-negative-va","errorCode":null,"errorMessage":"surface_area_cuboid() only accepts non-negative values","messagePattern":"surface_area_cuboid\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/area.py","lineNumber":55,"sourceCode":"    >>> 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\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\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: surface_area_cuboid() only accepts non-negative values\r\n    \"\"\"\r\n    if length < 0 or breadth < 0 or height < 0:\r\n        raise ValueError(\"surface_area_cuboid() only accepts non-negative values\")\r\n    return 2 * ((length * breadth) + (breadth * height) + (length * height))\r\n\r\n\r\ndef surface_area_sphere(radius: float) -> float:\r\n    \"\"\"\r\n    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","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/area.py#L37-L73","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Log which of length/breadth/height is negative before the call and fix the source of that value.","If dimensions come from coordinates, ensure you compute abs(max - min) or sort the pair before subtracting.","Replace sentinel values like -1 with None/0 at parse time.","Catch ValueError at the boundary to report all three values in an application error."],"exampleFix":"// before\narea = surface_area_cuboid(l, b, h)  # one of them may be -1 (missing)\n\n# after\nif min(length, breadth, height) < 0:\n    raise ValueError(f\"cuboid dimensions must be >= 0: {length}, {breadth}, {height}\")\narea = surface_area_cuboid(length, breadth, height)","handlingStrategy":"validation","validationCode":"if length < 0 or breadth < 0 or height < 0:\n    raise ValueError(f'cuboid dimensions must be >= 0: {length}, {breadth}, {height}')\narea = surface_area_cuboid(length, breadth, height)","typeGuard":"def is_valid_cuboid(dims: tuple) -> bool:\n    return all(isinstance(d, (int, float)) and d >= 0 for d in dims)","tryCatchPattern":"try:\n    area = surface_area_cuboid(l, b, h)\nexcept ValueError as e:\n    logger.error('cuboid input rejected: %s', (l, b, h))\n    raise","preventionTips":["Check min(length, breadth, height) >= 0 in one expression before calling.","Sort coordinate pairs before subtracting to keep spans non-negative.","Use pydantic Field(ge=0) on dimension models."],"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"}