{"record":{"id":"b544edf4a87e99b9","repo":"TheAlgorithms/Python","slug":"vol-pyramid-only-accepts-non-negative-values","errorCode":null,"errorMessage":"vol_pyramid() only accepts non-negative values","messagePattern":"vol_pyramid\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/volume.py","lineNumber":315,"sourceCode":"    >>> 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\n    >>> vol_pyramid(-1, 1)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_pyramid() only accepts non-negative values\n    >>> vol_pyramid(1, -1)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_pyramid() only accepts non-negative values\n    \"\"\"\n    if height < 0 or area_of_base < 0:\n        raise ValueError(\"vol_pyramid() only accepts non-negative values\")\n    return area_of_base * height / 3.0\n\n\ndef vol_sphere(radius: float) -> float:\n    r\"\"\"\n    | Calculate the Volume of a Sphere.\n    | Wikipedia reference: https://en.wikipedia.org/wiki/Sphere\n\n    :return: :math:`\\frac{4}{3} \\cdot \\pi \\cdot r^3`\n\n    >>> vol_sphere(5)\n    523.5987755982989\n    >>> vol_sphere(1)\n    4.1887902047863905\n    >>> vol_sphere(1.6)\n    17.15728467880506\n    >>> vol_sphere(0)\n    0.0","sourceCodeStart":297,"sourceCodeEnd":333,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/volume.py#L297-L333","documentation":"Raised by vol_pyramid(area_of_base, height) in maths/volume.py when height or area_of_base is negative. The function computes area_of_base * height / 3.0 and rejects negative inputs; zero values are allowed and return 0.0.","triggerScenarios":"Calling vol_pyramid(-1, 1) or vol_pyramid(1, -1); passing a signed base area from a polygon routine or a negative height from an inverted vertical delta.","commonSituations":"Same family of issues as vol_cone/vol_prism: winding-sensitive area computations and orientation-encoded heights; also copy-paste of test fixtures containing negative placeholder values.","solutions":["Apply abs() to signed base areas and height deltas before calling.","Validate at your data boundary (parser/DB layer) so negatives never reach geometry code.","Wrap the call in try/except ValueError to convert to a domain-specific error for end-user flows."],"exampleFix":"# before\nvol = vol_pyramid(base_area, tip_z - base_z)  # ValueError when tip below base\n\n# after\nvol = vol_pyramid(abs(base_area), abs(tip_z - base_z))","handlingStrategy":"validation","validationCode":"area = abs(area_of_base)\nh = abs(tip_z - base_z)\nvol = vol_pyramid(area, h)","typeGuard":null,"tryCatchPattern":"try:\n    vol = vol_pyramid(area, h)\nexcept ValueError:\n    log.warning('rejecting pyramid with negative base/height: %s', (area, h))\n    raise","preventionTips":["Same signed-area pitfall as cone/prism.","Validate imported fixtures for placeholder negative values."],"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"}