{"record":{"id":"a4cea27cc85cb2de","repo":"TheAlgorithms/Python","slug":"surface-area-torus-only-accepts-non-negative-val","errorCode":null,"errorMessage":"surface_area_torus() only accepts non-negative values","messagePattern":"surface_area_torus\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/area.py","lineNumber":231,"sourceCode":"    >>> surface_area_torus(3, 4)\r\n    Traceback (most recent call last):\r\n        ...\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","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/area.py#L213-L249","documentation":"surface_area_torus() raises this ValueError when torus_radius or tube_radius is negative. The library enforces non-negative radii before computing 4 * pi^2 * R * r. This is the first of two guards in the function — even after passing it, a second check rejects spindle/self-intersecting geometry (torus_radius < tube_radius).","triggerScenarios":"Calling surface_area_torus(torus_radius, tube_radius) with either argument < 0, e.g. surface_area_torus(-1, 1) or surface_area_torus(1, -1).","commonSituations":"CAD/game asset generation with parametric sweeps where a radius parameter goes negative at range extremes; sliders in a UI bound to radius values that can cross zero; parsing dims from JSON with sign errors.","solutions":["Clamp or validate both radii to >= 0 before the call.","If parameters come from optimization loops, bound the search domain to [0, inf).","Fix the sign at the data source rather than suppressing the error.","Catch ValueError to emit a clear message naming both radii."],"exampleFix":"// before\narea = surface_area_torus(R, r)  # R or r may go negative in a sweep\n\n# after\nR = max(R, 0.0)\nr = max(r, 0.0)\narea = surface_area_torus(R, r)","handlingStrategy":"validation","validationCode":"if torus_radius < 0 or tube_radius < 0:\n    raise ValueError(f'torus radii must be >= 0: R={torus_radius}, r={tube_radius}')\narea = surface_area_torus(torus_radius, tube_radius)","typeGuard":"def is_valid_torus_radii(R: float, r: float) -> bool:\n    return R >= 0 and r >= 0","tryCatchPattern":"try:\n    area = surface_area_torus(R, r)\nexcept ValueError as e:\n    raise ValueError(f'torus input rejected (R={R}, r={r}): {e}') from e","preventionTips":["Bound parametric sweeps to non-negative radii.","Clamp slider-driven parameters with max(x, 0.0) at the UI boundary."],"tags":["python","math","geometry","validation","valueerror","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}