{"record":{"id":"d38ed6f63f52e5ea","repo":"TheAlgorithms/Python","slug":"vol-cuboid-only-accepts-non-negative-values","errorCode":null,"errorMessage":"vol_cuboid() only accepts non-negative values","messagePattern":"vol_cuboid\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/volume.py","lineNumber":201,"sourceCode":"    >>> vol_cuboid(1.6, 2.6, 3.6)\n    14.976\n    >>> vol_cuboid(0, 0, 0)\n    0.0\n    >>> vol_cuboid(-1, 2, 3)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_cuboid() only accepts non-negative values\n    >>> vol_cuboid(1, -2, 3)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_cuboid() only accepts non-negative values\n    >>> vol_cuboid(1, 2, -3)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_cuboid() only accepts non-negative values\n    \"\"\"\n    if width < 0 or height < 0 or length < 0:\n        raise ValueError(\"vol_cuboid() only accepts non-negative values\")\n    return float(width * height * length)\n\n\ndef vol_cone(area_of_base: float, height: float) -> float:\n    r\"\"\"\n    | Calculate the Volume of a Cone.\n    | Wikipedia reference: https://en.wikipedia.org/wiki/Cone\n\n    :return: :math:`\\frac{1}{3} \\cdot area\\_of\\_base \\cdot height`\n\n    >>> vol_cone(10, 3)\n    10.0\n    >>> vol_cone(1, 1)\n    0.3333333333333333\n    >>> vol_cone(1.6, 1.6)\n    0.8533333333333335\n    >>> vol_cone(0, 0)\n    0.0","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/volume.py#L183-L219","documentation":"Raised by vol_cuboid(width, height, length) in maths/volume.py when any of the three dimensions is negative. A cuboid with a negative edge has no physical meaning, so the function guards all three inputs before returning float(width * height * length).","triggerScenarios":"Calling vol_cuboid(-1, 2, 3), vol_cuboid(1, -2, 3) or vol_cuboid(1, 2, -3); dimensions computed as differences (max_x - min_x) where the ordering is inverted.","commonSituations":"Box dimensions derived from min/max coordinates with the operands swapped; CSV or JSON data with sign errors; unit conversions (e.g. inches to a signed delta) producing negatives.","solutions":["Validate width, height, length are all >= 0 at the call site before invoking vol_cuboid.","When dimensions come from coordinate pairs, compute them as abs(max - min) or sort the pair first.","Sanitize input files/scripts so sign errors are caught at parse time with a clearer message."],"exampleFix":"# before\nvol = vol_cuboid(x2 - x1, y2 - y1, z2 - z1)  # ValueError if any max < min\n\n# after\nvol = vol_cuboid(abs(x2 - x1), abs(y2 - y1), abs(z2 - z1))","handlingStrategy":"validation","validationCode":"w, h, l = abs(x2 - x1), abs(y2 - y1), abs(z2 - z1)\nassert min(w, h, l) >= 0  # abs() already guarantees it\nvol = vol_cuboid(w, h, l)","typeGuard":null,"tryCatchPattern":"try:\n    vol = vol_cuboid(w, h, l)\nexcept ValueError:\n    log.error('cuboid dimensions must be non-negative: %s', (w, h, l))\n    raise","preventionTips":["Derive dimensions from sorted coordinate pairs or abs differences.","Check imported numeric columns for negatives before geometry code."],"tags":["math","validation","geometry","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}