{"record":{"id":"f4a24e7edcfdd2c1","repo":"TheAlgorithms/Python","slug":"vol-conical-frustum-only-accepts-non-negative-va","errorCode":null,"errorMessage":"vol_conical_frustum() only accepts non-negative values","messagePattern":"vol_conical_frustum\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/volume.py","lineNumber":472,"sourceCode":"    >>> vol_conical_frustum(0, 0, 0)\n    0.0\n    >>> vol_conical_frustum(-2, 2, 1)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_conical_frustum() only accepts non-negative values\n    >>> vol_conical_frustum(2, -2, 1)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_conical_frustum() only accepts non-negative values\n    >>> vol_conical_frustum(2, 2, -1)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_conical_frustum() only accepts non-negative values\n    \"\"\"\n    # Volume is 1/3 * pi * height *\n    #           (radius_1 squared + radius_2 squared + radius_1 * radius_2)\n    if radius_1 < 0 or radius_2 < 0 or height < 0:\n        raise ValueError(\"vol_conical_frustum() only accepts non-negative values\")\n    return (\n        1\n        / 3\n        * pi\n        * height\n        * (pow(radius_1, 2) + pow(radius_2, 2) + radius_1 * radius_2)\n    )\n\n\ndef vol_torus(torus_radius: float, tube_radius: float) -> float:\n    r\"\"\"\n    | Calculate the Volume of a Torus.\n    | Wikipedia reference: https://en.wikipedia.org/wiki/Torus\n\n    :return: :math:`2 \\pi^2 \\cdot torus\\_radius \\cdot tube\\_radius^2`\n\n    >>> vol_torus(1, 1)\n    19.739208802178716","sourceCodeStart":454,"sourceCodeEnd":490,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/volume.py#L454-L490","documentation":"Raised by vol_conical_frustum(height, radius_1, radius_2) in maths/volume.py when any of height, radius_1 or radius_2 is negative. The formula is 1/3 * pi * height * (r1^2 + r2^2 + r1*r2), guarded by the sign check; note the argument order is (height, radius_1, radius_2), height first.","triggerScenarios":"Calling vol_conical_frustum(2, -2, 1) or vol_conical_frustum(2, 2, -1); accidentally passing (radius, radius, height) because the height-first signature differs from sibling functions, producing a negative only indirectly — but any negative argument triggers it.","commonSituations":"Argument-order confusion with other volume functions that take radii first; signed heights from inverted elevation data; negative radii from mis-parsed files.","solutions":["Remember the signature is vol_conical_frustum(height, radius_1, radius_2) and validate all three are >= 0 before calling.","Use keyword arguments (height=..., radius_1=..., radius_2=...) to make ordering mistakes impossible.","Normalize signed heights to abs() at the source."],"exampleFix":"# before\nvol = vol_conical_frustum(r1, r2, h)  # wrong order: h lands in radius_2\n\n# after\nvol = vol_conical_frustum(height=h, radius_1=r1, radius_2=r2)","handlingStrategy":"validation","validationCode":"if height < 0 or radius_1 < 0 or radius_2 < 0:\n    raise ValueError(f'frustum args must be >= 0: h={height}, r1={radius_1}, r2={radius_2}')\nvol = vol_conical_frustum(height, radius_1, radius_2)","typeGuard":null,"tryCatchPattern":"try:\n    vol = vol_conical_frustum(height, radius_1, radius_2)\nexcept ValueError as e:\n    raise ValueError(f'bad frustum inputs h={height}, r1={radius_1}, r2={radius_2}') from e","preventionTips":["Signature is height-first, unlike most sibling functions — prefer keyword args.","Normalize signed heights before calling."],"tags":["math","validation","geometry","argument-order","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}