{"record":{"id":"599f21b7020cc4fa","repo":"TheAlgorithms/Python","slug":"area-triangle-three-sides-only-accepts-non-negat","errorCode":null,"errorMessage":"area_triangle_three_sides() only accepts non-negative values","messagePattern":"area_triangle_three_sides\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/area.py","lineNumber":350,"sourceCode":"    >>> area_triangle_three_sides(1, -2, 1)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: area_triangle_three_sides() only accepts non-negative values\r\n    >>> area_triangle_three_sides(2, 4, 7)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: Given three sides do not form a triangle\r\n    >>> area_triangle_three_sides(2, 7, 4)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: Given three sides do not form a triangle\r\n    >>> area_triangle_three_sides(7, 2, 4)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: Given three sides do not form a triangle\r\n    \"\"\"\r\n    if side1 < 0 or side2 < 0 or side3 < 0:\r\n        raise ValueError(\"area_triangle_three_sides() only accepts non-negative values\")\r\n    elif side1 + side2 < side3 or side1 + side3 < side2 or side2 + side3 < side1:\r\n        raise ValueError(\"Given three sides do not form a triangle\")\r\n    semi_perimeter = (side1 + side2 + side3) / 2\r\n    area = sqrt(\r\n        semi_perimeter\r\n        * (semi_perimeter - side1)\r\n        * (semi_perimeter - side2)\r\n        * (semi_perimeter - side3)\r\n    )\r\n    return area\r\n\r\n\r\ndef area_parallelogram(base: float, height: float) -> float:\r\n    \"\"\"\r\n    Calculate the area of a parallelogram.\r\n\r\n    >>> area_parallelogram(10, 20)\r\n    200\r","sourceCodeStart":332,"sourceCodeEnd":368,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/area.py#L332-L368","documentation":"area_triangle_three_sides() raises this ValueError when side1, side2, or side3 is negative. The function implements Heron's formula and first validates that all side lengths are non-negative. A negative side would make the expression under sqrt() meaningless, so the library rejects it before the separate triangle-inequality check.","triggerScenarios":"Calling area_triangle_three_sides(side1, side2, side3) with any argument < 0, e.g. area_triangle_three_sides(-1, 2, 4). The negativity check runs first, so a negative side raises this error even if the sides would otherwise violate the triangle inequality too.","commonSituations":"Side lengths from sensor measurements with sign glitches; distances computed via dot products that can be negative; test data generated over ranges that include negatives.","solutions":["Validate all three sides >= 0 before the call; fix the negative value at its source.","If sides are distances, ensure the distance routine returns abs() values.","Filter/clean datasets to drop or repair negative side lengths.","Catch ValueError and distinguish it from the triangle-inequality error for clearer diagnostics."],"exampleFix":"// before\narea = area_triangle_three_sides(a, b, c)  # a may be -1\n\n# after\nif a < 0 or b < 0 or c < 0:\n    raise ValueError(f'sides must be non-negative: {a}, {b}, {c}')\narea = area_triangle_three_sides(a, b, c)","handlingStrategy":"validation","validationCode":"if side1 < 0 or side2 < 0 or side3 < 0:\n    raise ValueError(f'sides must be non-negative: {side1}, {side2}, {side3}')\narea = area_triangle_three_sides(side1, side2, side3)","typeGuard":"def has_non_negative_sides(sides: tuple) -> bool:\n    return all(s >= 0 for s in sides)","tryCatchPattern":"try:\n    area = area_triangle_three_sides(a, b, c)\nexcept ValueError as e:\n    raise ValueError(f'invalid triangle sides {(a, b, c)}: {e}') from e","preventionTips":["Distance routines should always return non-negative values; check yours.","Sanitize generated test data ranges to exclude negative sides."],"tags":["python","math","geometry","validation","valueerror","herons-formula"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}