{"record":{"id":"facf097925db8d1f","repo":"TheAlgorithms/Python","slug":"vol-prism-only-accepts-non-negative-values","errorCode":null,"errorMessage":"vol_prism() only accepts non-negative values","messagePattern":"vol_prism\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/volume.py","lineNumber":286,"sourceCode":"    >>> vol_prism(10, 2)\n    20.0\n    >>> vol_prism(11, 1)\n    11.0\n    >>> vol_prism(1.6, 1.6)\n    2.5600000000000005\n    >>> vol_prism(0, 0)\n    0.0\n    >>> vol_prism(-1, 1)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_prism() only accepts non-negative values\n    >>> vol_prism(1, -1)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_prism() only accepts non-negative values\n    \"\"\"\n    if height < 0 or area_of_base < 0:\n        raise ValueError(\"vol_prism() only accepts non-negative values\")\n    return float(area_of_base * height)\n\n\ndef vol_pyramid(area_of_base: float, height: float) -> float:\n    r\"\"\"\n    | Calculate the Volume of a Pyramid.\n    | Wikipedia reference: https://en.wikipedia.org/wiki/Pyramid_(geometry)\n\n    :return: :math:`\\frac{1}{3} \\cdot B \\cdot h`\n\n    >>> vol_pyramid(10, 3)\n    10.0\n    >>> vol_pyramid(1.5, 3)\n    1.5\n    >>> vol_pyramid(1.6, 1.6)\n    0.8533333333333335\n    >>> vol_pyramid(0, 0)\n    0.0","sourceCodeStart":268,"sourceCodeEnd":304,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/volume.py#L268-L304","documentation":"Raised by vol_prism(area_of_base, height) in maths/volume.py when height or area_of_base is negative. The function returns float(area_of_base * height) and validates both arguments first; zero inputs are valid and yield 0.0.","triggerScenarios":"Calling vol_prism(-1, 1) or vol_prism(1, -1); feeding a signed polygon area from a winding-sensitive algorithm, or a negative height from inverted elevation data.","commonSituations":"Signed base areas from cross products that flip sign with vertex order; terrain/building models where 'downward' extrusion is encoded as negative height; unvalidated spreadsheet imports.","solutions":["Normalize signed areas with abs() before the call.","Treat downward extrusion explicitly as a magnitude (abs(depth)) and track direction in your own logic.","Validate imported numeric columns for negatives before geometry processing."],"exampleFix":"# before\nvol = vol_prism(signed_area, depth)  # ValueError if depth < 0\n\n# after\nvol = vol_prism(abs(signed_area), abs(depth))","handlingStrategy":"validation","validationCode":"area = abs(signed_area)\nh = abs(height)\nvol = vol_prism(area, h)","typeGuard":null,"tryCatchPattern":"try:\n    vol = vol_prism(area, h)\nexcept ValueError as e:\n    raise ValueError(f'invalid prism inputs: area={area}, h={h}') from e","preventionTips":["Normalize cross-product/shoelace areas with abs().","Encode extrusion direction separately from the height magnitude."],"tags":["math","validation","geometry","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}