{"record":{"id":"24161d7027adffaa","repo":"TheAlgorithms/Python","slug":"area-circle-only-accepts-non-negative-values","errorCode":null,"errorMessage":"area_circle() only accepts non-negative values","messagePattern":"area_circle\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/area.py","lineNumber":451,"sourceCode":"\r\n\r\ndef area_circle(radius: float) -> float:\r\n    \"\"\"\r\n    Calculate the area of a circle.\r\n\r\n    >>> area_circle(20)\r\n    1256.6370614359173\r\n    >>> area_circle(1.6)\r\n    8.042477193189871\r\n    >>> area_circle(0)\r\n    0.0\r\n    >>> area_circle(-1)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: area_circle() only accepts non-negative values\r\n    \"\"\"\r\n    if radius < 0:\r\n        raise ValueError(\"area_circle() only accepts non-negative values\")\r\n    return pi * radius**2\r\n\r\n\r\ndef area_ellipse(radius_x: float, radius_y: float) -> float:\r\n    \"\"\"\r\n    Calculate the area of a ellipse.\r\n\r\n    >>> area_ellipse(10, 10)\r\n    314.1592653589793\r\n    >>> area_ellipse(10, 20)\r\n    628.3185307179587\r\n    >>> area_ellipse(0, 0)\r\n    0.0\r\n    >>> area_ellipse(1.6, 2.6)\r\n    13.06902543893354\r\n    >>> area_ellipse(-10, 20)\r\n    Traceback (most recent call last):\r\n        ...\r","sourceCodeStart":433,"sourceCodeEnd":469,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/area.py#L433-L469","documentation":"area_circle() raises this ValueError when radius is negative. The library requires radius >= 0 before returning pi * radius**2. Because squaring would silently hide a negative input, the guard exists specifically to surface that mistake.","triggerScenarios":"Calling area_circle(radius) with radius < 0, e.g. area_circle(-1).","commonSituations":"Radius computed as a coordinate delta; user/CLI input not restricted to non-negative; data files where '-' marks missing entries.","solutions":["Validate radius >= 0 before the call and correct the upstream sign error.","Pass abs(radius) when the value is a magnitude with a sign artifact.","Restrict input types in forms/CLI (reject negatives at parse time).","Catch ValueError to emit a clear message."],"exampleFix":"// before\narea = area_circle(r)  # r = point_x - center_x, may be negative\n\n# after\narea = area_circle(abs(point_x - center_x))","handlingStrategy":"validation","validationCode":"if radius < 0:\n    raise ValueError(f'circle radius must be >= 0, got {radius}')\narea = area_circle(radius)","typeGuard":"def is_valid_radius(r: object) -> bool:\n    return isinstance(r, (int, float)) and not isinstance(r, bool) and r >= 0","tryCatchPattern":"try:\n    area = area_circle(radius)\nexcept ValueError as e:\n    raise ValueError(f'invalid circle radius {radius!r}: {e}') from e","preventionTips":["Restrict CLI/form radius inputs to non-negative parsers.","Never rely on squaring to neutralize a negative radius."],"tags":["python","math","geometry","validation","valueerror","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}