{"record":{"id":"55d4f4ffbf3bb3e3","repo":"TheAlgorithms/Python","slug":"surface-area-torus-does-not-support-spindle-or-s","errorCode":null,"errorMessage":"surface_area_torus() does not support spindle or self intersecting tori","messagePattern":"surface_area_torus\\(\\) does not support spindle or self intersecting tori","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/area.py","lineNumber":233,"sourceCode":"        ...\r\n    ValueError: surface_area_torus() does not support spindle or self intersecting tori\r\n    >>> surface_area_torus(1.6, 1.6)\r\n    101.06474906715503\r\n    >>> surface_area_torus(0, 0)\r\n    0.0\r\n    >>> surface_area_torus(-1, 1)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: surface_area_torus() only accepts non-negative values\r\n    >>> surface_area_torus(1, -1)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: surface_area_torus() only accepts non-negative values\r\n    \"\"\"\r\n    if torus_radius < 0 or tube_radius < 0:\r\n        raise ValueError(\"surface_area_torus() only accepts non-negative values\")\r\n    if torus_radius < tube_radius:\r\n        raise ValueError(\r\n            \"surface_area_torus() does not support spindle or self intersecting tori\"\r\n        )\r\n    return 4 * pow(pi, 2) * torus_radius * tube_radius\r\n\r\n\r\ndef area_rectangle(length: float, width: float) -> float:\r\n    \"\"\"\r\n    Calculate the area of a rectangle.\r\n\r\n    >>> area_rectangle(10, 20)\r\n    200\r\n    >>> area_rectangle(1.6, 2.6)\r\n    4.16\r\n    >>> area_rectangle(0, 0)\r\n    0\r\n    >>> area_rectangle(-1, -2)\r\n    Traceback (most recent call last):\r\n        ...\r","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/area.py#L215-L251","documentation":"surface_area_torus() raises this ValueError when torus_radius < tube_radius, i.e. the tube is thicker than the ring it wraps. Such a torus is a spindle or self-intersecting torus, for which the simple formula 4 * pi^2 * R * r is not the (outer) surface area, so the library refuses to compute a wrong answer. Note: torus_radius == tube_radius (horn torus) is allowed.","triggerScenarios":"Calling surface_area_torus(R, r) with R < r, e.g. surface_area_torus(2, 3). Often caused by swapping the two positional arguments, since both are just radii.","commonSituations":"Argument-order confusion because both parameters are radii with no type distinction; parametric sweeps that let the tube grow past the ring radius; UI sliders without a constraint linking the two values.","solutions":["Check torus_radius >= tube_radius before calling; if the arguments were swapped, swap them back.","Constrain parameter sweeps/UI sliders so tube_radius can never exceed torus_radius.","If you genuinely need a spindle/self-intersecting torus area, use a geometry kernel (e.g. trimesh/CGAL) instead of this function.","Catch ValueError and report both radii in the message."],"exampleFix":"// before\narea = surface_area_torus(tube, ring)  # arguments swapped\n\n# after\narea = surface_area_torus(ring, tube)  # ring >= tube\n\n# or guard explicitly:\nif tube > ring:\n    raise ValueError('tube radius must not exceed torus radius')","handlingStrategy":"validation","validationCode":"if torus_radius < tube_radius:\n    raise ValueError(\n        f'spindle torus not supported: torus_radius={torus_radius} < tube_radius={tube_radius}'\n    )\narea = surface_area_torus(torus_radius, tube_radius)","typeGuard":"def is_ring_torus(R: float, r: float) -> bool:\n    return R >= r","tryCatchPattern":"try:\n    area = surface_area_torus(R, r)\nexcept ValueError as e:\n    if 'spindle' in str(e):\n        raise ValueError(f'geometry not supported, swap or resize radii: R={R}, r={r}') from e\n    raise","preventionTips":["Remember the constraint R >= r (horn torus R == r is fine).","Link UI sliders so tube_radius can never exceed torus_radius.","For spindle/self-intersecting tori, use a mesh-based geometry library instead."],"tags":["python","math","geometry","validation","valueerror","domain-error","torus"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}