{"record":{"id":"9f5d0734118c4132","repo":"TheAlgorithms/Python","slug":"vol-cone-only-accepts-non-negative-values","errorCode":null,"errorMessage":"vol_cone() only accepts non-negative values","messagePattern":"vol_cone\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/volume.py","lineNumber":230,"sourceCode":"    >>> 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\n    >>> vol_cone(-1, 1)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_cone() only accepts non-negative values\n    >>> vol_cone(1, -1)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_cone() only accepts non-negative values\n    \"\"\"\n    if height < 0 or area_of_base < 0:\n        raise ValueError(\"vol_cone() only accepts non-negative values\")\n    return area_of_base * height / 3.0\n\n\ndef vol_right_circ_cone(radius: float, height: float) -> float:\n    r\"\"\"\n    | Calculate the Volume of a Right Circular Cone.\n    | Wikipedia reference: https://en.wikipedia.org/wiki/Cone\n\n    :return: :math:`\\frac{1}{3} \\cdot \\pi \\cdot radius^2 \\cdot height`\n\n    >>> vol_right_circ_cone(2, 3)\n    12.566370614359172\n    >>> vol_right_circ_cone(0, 0)\n    0.0\n    >>> vol_right_circ_cone(1.6, 1.6)\n    4.289321169701265\n    >>> vol_right_circ_cone(-1, 1)\n    Traceback (most recent call last):","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/volume.py#L212-L248","documentation":"Raised by vol_cone(area_of_base, height) in maths/volume.py when height or area_of_base is negative. The formula area_of_base * height / 3.0 is only meaningful for non-negative base area and height, so both are guarded. Zero values are accepted and return 0.0.","triggerScenarios":"Calling vol_cone(-1, 1) or vol_cone(1, -1); passing a signed area computed from a cross product whose orientation flipped, or a height derived from a coordinate delta with inverted ordering.","commonSituations":"Signed area inputs from vector math (shoelace formula can yield negatives depending on vertex winding); height computed as top - bottom where top < bottom; raw measurement data with typos.","solutions":["Take abs() of signed areas from cross-product/shoelace computations before passing them in.","Compute heights as a non-negative delta (abs(top - bottom) or sorted endpoints).","Add an up-front assert/raise at your data layer so invalid negatives fail with your own message instead of the library's."],"exampleFix":"# before\nvol = vol_cone(shoelace_area(points), h)  # ValueError for clockwise winding\n\n# after\nvol = vol_cone(abs(shoelace_area(points)), abs(h))","handlingStrategy":"validation","validationCode":"area = abs(area_of_base)\nheight = abs(height)\nif area < 0 or height < 0:  # unreachable after abs(), kept for clarity\n    raise ValueError\nvol = vol_cone(area, height)","typeGuard":null,"tryCatchPattern":"try:\n    vol = vol_cone(area, height)\nexcept ValueError as e:\n    raise ValueError(f'bad cone inputs area={area}, height={height}') from e","preventionTips":["Signed polygon areas must be abs()-ed (winding order flips sign).","Heights from coordinate deltas should be magnitudes."],"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"}