{"record":{"id":"8f3491f15033ad49","repo":"TheAlgorithms/Python","slug":"surface-area-cylinder-only-accepts-non-negative","errorCode":null,"errorMessage":"surface_area_cylinder() only accepts non-negative values","messagePattern":"surface_area_cylinder\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/area.py","lineNumber":201,"sourceCode":"    >>> surface_area_cylinder(0, 0)\r\n    0.0\r\n    >>> surface_area_cylinder(6, 8)\r\n    527.7875658030853\r\n    >>> surface_area_cylinder(-1, -2)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: surface_area_cylinder() only accepts non-negative values\r\n    >>> surface_area_cylinder(1, -2)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: surface_area_cylinder() only accepts non-negative values\r\n    >>> surface_area_cylinder(-1, 2)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: surface_area_cylinder() only accepts non-negative values\r\n    \"\"\"\r\n    if radius < 0 or height < 0:\r\n        raise ValueError(\"surface_area_cylinder() only accepts non-negative values\")\r\n    return 2 * pi * radius * (height + radius)\r\n\r\n\r\ndef surface_area_torus(torus_radius: float, tube_radius: float) -> float:\r\n    \"\"\"Calculate the Area of a Torus.\r\n    Wikipedia reference: https://en.wikipedia.org/wiki/Torus\r\n    :return 4pi^2 * torus_radius * tube_radius\r\n    >>> surface_area_torus(1, 1)\r\n    39.47841760435743\r\n    >>> surface_area_torus(4, 3)\r\n    473.7410112522892\r\n    >>> 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","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/area.py#L183-L219","documentation":"surface_area_cylinder() raises this ValueError when radius or height is negative. Both are physical dimensions enforced >= 0 before returning 2 * pi * r * (h + r). The guard exists because the formula would otherwise happily produce nonsense for negative inputs (or negative areas).","triggerScenarios":"Calling surface_area_cylinder(radius, height) with radius < 0 or height < 0, e.g. surface_area_cylinder(-1, 2) or surface_area_cylinder(1, -2).","commonSituations":"Tank/pipe sizing code where height is computed as outlet_level - inlet_level and can go negative when sensors report out of order; user form input not restricted to positive numbers; importing from spreadsheets with negative placeholders.","solutions":["Assert or check radius >= 0 and height >= 0 before calling; fix the negative source.","For level-derived heights, clamp or reorder: height = abs(top - bottom).","Add min=0 constraints in UI forms or schema validators (pydantic Field(ge=0)).","Catch ValueError and re-raise with your application's error context."],"exampleFix":"// before\narea = surface_area_cylinder(r, h)  # h = level_a - level_b may be negative\n\n# after\nh = abs(level_a - level_b)\narea = surface_area_cylinder(r, h)","handlingStrategy":"validation","validationCode":"if radius < 0 or height < 0:\n    raise ValueError(f'cylinder dimensions must be >= 0: radius={radius}, height={height}')\narea = surface_area_cylinder(radius, height)","typeGuard":"def is_valid_cylinder(r: float, h: float) -> bool:\n    return r >= 0 and h >= 0","tryCatchPattern":"try:\n    area = surface_area_cylinder(radius, height)\nexcept ValueError as e:\n    raise ValueError(f'cylinder input rejected: {e}') from e","preventionTips":["For tank levels, compute height as abs(top - bottom) or clamp to 0.","Enforce ge=0 in schema/pydantic models for physical dimensions."],"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"}