{"record":{"id":"f04b895b987fb326","repo":"TheAlgorithms/Python","slug":"vol-icosahedron-only-accepts-non-negative-values","errorCode":null,"errorMessage":"vol_icosahedron() only accepts non-negative values","messagePattern":"vol_icosahedron\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/volume.py","lineNumber":539,"sourceCode":"    >>> isclose(vol_icosahedron(10), 2181.694990624912374)\n    True\n    >>> isclose(vol_icosahedron(5), 272.711873828114047)\n    True\n    >>> isclose(vol_icosahedron(3.49), 92.740688412033628)\n    True\n    >>> vol_icosahedron(0)\n    0.0\n    >>> vol_icosahedron(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_icosahedron() only accepts non-negative values\n    >>> vol_icosahedron(-0.2)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_icosahedron() only accepts non-negative values\n    \"\"\"\n    if tri_side < 0:\n        raise ValueError(\"vol_icosahedron() only accepts non-negative values\")\n    return tri_side**3 * (3 + 5**0.5) * 5 / 12\n\n\ndef main():\n    \"\"\"Print the Results of Various Volume Calculations.\"\"\"\n    print(\"Volumes:\")\n    print(f\"Cube: {vol_cube(2) = }\")  # = 8\n    print(f\"Cuboid: {vol_cuboid(2, 2, 2) = }\")  # = 8\n    print(f\"Cone: {vol_cone(2, 2) = }\")  # ~= 1.33\n    print(f\"Right Circular Cone: {vol_right_circ_cone(2, 2) = }\")  # ~= 8.38\n    print(f\"Prism: {vol_prism(2, 2) = }\")  # = 4\n    print(f\"Pyramid: {vol_pyramid(2, 2) = }\")  # ~= 1.33\n    print(f\"Sphere: {vol_sphere(2) = }\")  # ~= 33.5\n    print(f\"Hemisphere: {vol_hemisphere(2) = }\")  # ~= 16.75\n    print(f\"Circular Cylinder: {vol_circular_cylinder(2, 2) = }\")  # ~= 25.1\n    print(f\"Torus: {vol_torus(2, 2) = }\")  # ~= 157.9\n    print(f\"Conical Frustum: {vol_conical_frustum(2, 2, 4) = }\")  # ~= 58.6\n    print(f\"Spherical cap: {vol_spherical_cap(1, 2) = }\")  # ~= 5.24","sourceCodeStart":521,"sourceCodeEnd":557,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/volume.py#L521-L557","documentation":"Raised by vol_icosahedron(tri_side) in maths/volume.py when the triangle edge length is negative. The function returns tri_side**3 * (3 + sqrt(5)) * 5 / 12 after the guard; tri_side == 0 is valid and returns 0.0.","triggerScenarios":"Calling vol_icosahedron(-1) or vol_icosahedron(-0.2); edge lengths from user input or mesh data with a negative sign.","commonSituations":"Parsing edge lengths from strings like '-0.2'; direction-signed edge vectors used directly as lengths; default sentinel values of -1 leaking into geometry calls.","solutions":["Validate tri_side >= 0 before the call; reject negative lengths at input parsing with your own message.","Replace sentinel defaults (-1 meaning 'unset') with None and check for them explicitly.","Convert signed vector differences to magnitudes (abs or norm) before using as edge lengths."],"exampleFix":"# before\nside = row.get('edge', -1)  # -1 sentinel\nvol = vol_icosahedron(side)  # ValueError when row missing\n\n# after\nside = row.get('edge')\nif side is None or side < 0:\n    raise ValueError(f\"invalid edge length in row: {side!r}\")\nvol = vol_icosahedron(side)","handlingStrategy":"type-guard","validationCode":"def is_valid_edge(side) -> bool:\n    return isinstance(side, (int, float)) and not isinstance(side, bool) and side >= 0","typeGuard":"def is_valid_edge(side) -> TypeGuard[float]:\n    return isinstance(side, (int, float)) and not isinstance(side, bool) and side >= 0","tryCatchPattern":"try:\n    v = vol_icosahedron(side)\nexcept ValueError as e:\n    raise ValueError(f'edge length must be >= 0, got {side}') from e","preventionTips":["Do not use -1 as an 'unset' sentinel that can reach geometry calls.","Convert vector differences to lengths with abs/norm."],"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"}