{"record":{"id":"45f581783bed2268","repo":"TheAlgorithms/Python","slug":"area-triangle-only-accepts-non-negative-values","errorCode":null,"errorMessage":"area_triangle() only accepts non-negative values","messagePattern":"area_triangle\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/area.py","lineNumber":311,"sourceCode":"    >>> area_triangle(1.6, 2.6)\r\n    2.08\r\n    >>> area_triangle(0, 0)\r\n    0.0\r\n    >>> area_triangle(-1, -2)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: area_triangle() only accepts non-negative values\r\n    >>> area_triangle(1, -2)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: area_triangle() only accepts non-negative values\r\n    >>> area_triangle(-1, 2)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: area_triangle() only accepts non-negative values\r\n    \"\"\"\r\n    if base < 0 or height < 0:\r\n        raise ValueError(\"area_triangle() only accepts non-negative values\")\r\n    return (base * height) / 2\r\n\r\n\r\ndef area_triangle_three_sides(side1: float, side2: float, side3: float) -> float:\r\n    \"\"\"\r\n    Calculate area of triangle when the length of 3 sides are known.\r\n    This function uses Heron's formula: https://en.wikipedia.org/wiki/Heron%27s_formula\r\n\r\n    >>> area_triangle_three_sides(5, 12, 13)\r\n    30.0\r\n    >>> area_triangle_three_sides(10, 11, 12)\r\n    51.521233486786784\r\n    >>> area_triangle_three_sides(0, 0, 0)\r\n    0.0\r\n    >>> area_triangle_three_sides(1.6, 2.6, 3.6)\r\n    1.8703742940919619\r\n    >>> area_triangle_three_sides(-1, -2, -1)\r\n    Traceback (most recent call last):\r","sourceCodeStart":293,"sourceCodeEnd":329,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/area.py#L293-L329","documentation":"area_triangle() raises this ValueError when base or height is negative. Both are physical measurements used in (base * height) / 2; the library rejects negatives so a bad sign cannot corrupt the result. (In signed-area shoelace contexts you would use a different routine — this function is for plain geometric area.)","triggerScenarios":"Calling area_triangle(base, height) with base < 0 or height < 0, e.g. area_triangle(-1, 2) or area_triangle(1, -2).","commonSituations":"Using coordinate deltas as base/height where direction flips sign; porting shoelace-formula code that legitimately produces signed heights; measurement data with negative placeholders.","solutions":["Pass magnitudes: abs(base), abs(height) when computing unsigned geometric area from coordinates.","Or use a coordinate-based formula (e.g. shoelace) instead of base*height/2 if signs matter.","Validate inputs at ingestion to reject negatives.","Catch ValueError and include the offending values in your error message."],"exampleFix":"// before\narea = area_triangle(x2 - x1, y2 - y1)  # deltas may be negative\n\n# after\narea = area_triangle(abs(x2 - x1), abs(y2 - y1))","handlingStrategy":"validation","validationCode":"if base < 0 or height < 0:\n    raise ValueError(f'triangle base/height must be >= 0: {base}, {height}')\narea = area_triangle(base, height)","typeGuard":"def is_valid_triangle_dims(b: float, h: float) -> bool:\n    return b >= 0 and h >= 0","tryCatchPattern":"try:\n    area = area_triangle(base, height)\nexcept ValueError as e:\n    raise ValueError(f'invalid triangle input: {e}') from e","preventionTips":["For coordinates, pass abs(deltas) or switch to a shoelace-based area function.","Don't feed signed shoelace terms into this unsigned-area API."],"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"}