{"record":{"id":"8bee632012732635","repo":"TheAlgorithms/Python","slug":"vol-torus-only-accepts-non-negative-values","errorCode":null,"errorMessage":"vol_torus() only accepts non-negative values","messagePattern":"vol_torus\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/volume.py","lineNumber":509,"sourceCode":"    >>> vol_torus(4, 3)\n    710.6115168784338\n    >>> vol_torus(3, 4)\n    947.4820225045784\n    >>> vol_torus(1.6, 1.6)\n    80.85179925372404\n    >>> vol_torus(0, 0)\n    0.0\n    >>> vol_torus(-1, 1)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_torus() only accepts non-negative values\n    >>> vol_torus(1, -1)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_torus() only accepts non-negative values\n    \"\"\"\n    if torus_radius < 0 or tube_radius < 0:\n        raise ValueError(\"vol_torus() only accepts non-negative values\")\n    return 2 * pow(pi, 2) * torus_radius * pow(tube_radius, 2)\n\n\ndef vol_icosahedron(tri_side: float) -> float:\n    \"\"\"\n    | Calculate the Volume of an Icosahedron.\n    | Wikipedia reference: https://en.wikipedia.org/wiki/Regular_icosahedron\n\n    >>> from math import isclose\n    >>> isclose(vol_icosahedron(2.5), 34.088984228514256)\n    True\n    >>> 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)","sourceCodeStart":491,"sourceCodeEnd":527,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/volume.py#L491-L527","documentation":"Raised by vol_torus(torus_radius, tube_radius) in maths/volume.py when either radius is negative. The function returns 2 * pi**2 * torus_radius * tube_radius**2; (0, 0) is valid and returns 0.0.","triggerScenarios":"Calling vol_torus(-1, 1) or vol_torus(1, -1); torus parameters from CAD exports or parametric sweep code where one radius went negative during iteration.","commonSituations":"Optimization or parameter-sweep loops that step a radius through negative values; misordered arguments with a signed tube radius; unvalidated user parameters for 3D models.","solutions":["Bound parameter sweeps to non-negative ranges before calling vol_torus.","Validate both radii >= 0 at the point values enter your program (CLI args, config, mesh import).","Skip or clamp iterations that would produce negative radii rather than calling the function."],"exampleFix":"# before\nfor tube_r in numpy.arange(-2, 2, 0.1):\n    v = vol_torus(R, tube_r)  # ValueError on negative tube_r\n\n# after\nfor tube_r in numpy.arange(0, 2, 0.1):\n    v = vol_torus(R, tube_r)","handlingStrategy":"validation","validationCode":"if torus_radius < 0 or tube_radius < 0:\n    raise ValueError(f'torus radii must be >= 0: {torus_radius}, {tube_radius}')\nvol = vol_torus(torus_radius, tube_radius)","typeGuard":null,"tryCatchPattern":"try:\n    v = vol_torus(R, r)\nexcept ValueError:\n    continue  # in sweeps: skip invalid parameter combos","preventionTips":["Constrain parameter-sweep ranges to non-negative values.","Validate 3D model parameters at load time."],"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"}