{"record":{"id":"2678c6b870e96564","repo":"TheAlgorithms/Python","slug":"area-trapezium-only-accepts-non-negative-values","errorCode":null,"errorMessage":"area_trapezium() only accepts non-negative values","messagePattern":"area_trapezium\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/area.py","lineNumber":431,"sourceCode":"    >>> area_trapezium(1, 2, -3)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: area_trapezium() only accepts non-negative values\r\n    >>> area_trapezium(-1, -2, 3)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: area_trapezium() only accepts non-negative values\r\n    >>> area_trapezium(1, -2, -3)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: area_trapezium() only accepts non-negative values\r\n    >>> area_trapezium(-1, 2, -3)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: area_trapezium() only accepts non-negative values\r\n    \"\"\"\r\n    if base1 < 0 or base2 < 0 or height < 0:\r\n        raise ValueError(\"area_trapezium() only accepts non-negative values\")\r\n    return 1 / 2 * (base1 + base2) * height\r\n\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","sourceCodeStart":413,"sourceCodeEnd":449,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/area.py#L413-L449","documentation":"area_trapezium() raises this ValueError when base1, base2, or height is negative. The library validates all three dimensions before returning 1/2 * (base1 + base2) * height. Note that the two bases may be passed in either order — only negativity, not ordering, is rejected.","triggerScenarios":"Calling area_trapezium(base1, base2, height) with any argument < 0, e.g. area_trapezium(-1, 2, -3) or area_trapezium(1, -2, -3).","commonSituations":"Cross-section dimensions in earthwork/volume calculations where a depth goes negative when terrain is above a datum; spreadsheet imports with negative placeholders; argument-order experiments that pass signed deltas.","solutions":["Check min(base1, base2, height) >= 0 before the call and repair the negative dimension.","For depth-like heights measured against a datum, subtract in a fixed direction or use abs().","Sanitize data at import time.","Catch ValueError and name the offending parameter."],"exampleFix":"// before\narea = area_trapezium(b1, b2, h)  # h = surface - datum may be negative\n\n# after\nh = abs(surface - datum)\narea = area_trapezium(b1, b2, h)","handlingStrategy":"validation","validationCode":"if base1 < 0 or base2 < 0 or height < 0:\n    raise ValueError(f'trapezium dimensions must be >= 0: {base1}, {base2}, {height}')\narea = area_trapezium(base1, base2, height)","typeGuard":"def is_valid_trapezium(b1: float, b2: float, h: float) -> bool:\n    return min(b1, b2, h) >= 0","tryCatchPattern":"try:\n    area = area_trapezium(base1, base2, height)\nexcept ValueError as e:\n    raise ValueError(f'trapezium input rejected: {e}') from e","preventionTips":["Measure depths against a datum in a fixed direction; use abs() when unsure.","Base order is irrelevant to validity; only sign matters."],"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"}