{"record":{"id":"42c7dfe9302da1e0","repo":"TheAlgorithms/Python","slug":"area-rectangle-only-accepts-non-negative-values","errorCode":null,"errorMessage":"area_rectangle() only accepts non-negative values","messagePattern":"area_rectangle\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/area.py","lineNumber":263,"sourceCode":"    >>> 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\n    ValueError: area_rectangle() only accepts non-negative values\r\n    >>> area_rectangle(1, -2)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: area_rectangle() only accepts non-negative values\r\n    >>> area_rectangle(-1, 2)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: area_rectangle() only accepts non-negative values\r\n    \"\"\"\r\n    if length < 0 or width < 0:\r\n        raise ValueError(\"area_rectangle() only accepts non-negative values\")\r\n    return length * width\r\n\r\n\r\ndef area_square(side_length: float) -> float:\r\n    \"\"\"\r\n    Calculate the area of a square.\r\n\r\n    >>> area_square(10)\r\n    100\r\n    >>> area_square(0)\r\n    0\r\n    >>> area_square(1.6)\r\n    2.5600000000000005\r\n    >>> area_square(-1)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: area_square() only accepts non-negative values\r\n    \"\"\"\r","sourceCodeStart":245,"sourceCodeEnd":281,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/area.py#L245-L281","documentation":"area_rectangle() raises this ValueError when length or width is negative. The library enforces non-negative side lengths before returning length * width. Although a negative times a negative would give a positive number, that result would be meaningless as an area, so the input is rejected up front.","triggerScenarios":"Calling area_rectangle(length, width) with length < 0 or width < 0, e.g. area_rectangle(-1, 2) or area_rectangle(1, -2).","commonSituations":"Room/plot dimensions from survey data with sign conventions (e.g. southward as negative); spreadsheet -1 sentinels for missing widths; differences of coordinates used directly as lengths.","solutions":["Validate length >= 0 and width >= 0 before the call; correct the sign at the source.","Convert signed measurements to magnitudes with abs() when direction is irrelevant.","Replace -1 sentinels with explicit missing-value handling during import.","Catch ValueError for user-facing reporting."],"exampleFix":"// before\narea = area_rectangle(l, w)  # l = x2 - x1 may be negative\n\n# after\nl, w = abs(x2 - x1), abs(y2 - y1)\narea = area_rectangle(l, w)","handlingStrategy":"validation","validationCode":"if length < 0 or width < 0:\n    raise ValueError(f'rectangle sides must be >= 0: {length}, {width}')\narea = area_rectangle(length, width)","typeGuard":"def is_valid_rect_sides(l: float, w: float) -> bool:\n    return l >= 0 and w >= 0","tryCatchPattern":"try:\n    area = area_rectangle(length, width)\nexcept ValueError as e:\n    raise ValueError(f'invalid rectangle input: {e}') from e","preventionTips":["Convert signed surveying deltas to magnitudes before use.","Replace -1 sentinels with None at import time."],"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"}