{"record":{"id":"38c815f5566c3509","repo":"TheAlgorithms/Python","slug":"area-rhombus-only-accepts-non-negative-values","errorCode":null,"errorMessage":"area_rhombus() only accepts non-negative values","messagePattern":"area_rhombus\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/area.py","lineNumber":509,"sourceCode":"    >>> area_rhombus(1.6, 2.6)\r\n    2.08\r\n    >>> area_rhombus(0, 0)\r\n    0.0\r\n    >>> area_rhombus(-1, -2)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: area_rhombus() only accepts non-negative values\r\n    >>> area_rhombus(1, -2)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: area_rhombus() only accepts non-negative values\r\n    >>> area_rhombus(-1, 2)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: area_rhombus() only accepts non-negative values\r\n    \"\"\"\r\n    if diagonal_1 < 0 or diagonal_2 < 0:\r\n        raise ValueError(\"area_rhombus() only accepts non-negative values\")\r\n    return 1 / 2 * diagonal_1 * diagonal_2\r\n\r\n\r\ndef area_reg_polygon(sides: int, length: float) -> float:\r\n    \"\"\"\r\n    Calculate the area of a regular polygon.\r\n    Wikipedia reference: https://en.wikipedia.org/wiki/Polygon#Regular_polygons\r\n    Formula: (n*s^2*cot(pi/n))/4\r\n\r\n    >>> area_reg_polygon(3, 10)\r\n    43.301270189221945\r\n    >>> area_reg_polygon(4, 10)\r\n    100.00000000000001\r\n    >>> area_reg_polygon(0, 0)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: area_reg_polygon() only accepts integers greater than or equal to \\\r\nthree as number of sides\r","sourceCodeStart":491,"sourceCodeEnd":527,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/area.py#L491-L527","documentation":"area_rhombus() raises this ValueError when diagonal_1 or diagonal_2 is negative. Diagonals are physical lengths; the library requires both >= 0 before returning 1/2 * d1 * d2. As with other product-based formulas, the guard prevents two negative inputs from silently yielding a positive, meaningless area.","triggerScenarios":"Calling area_rhombus(diagonal_1, diagonal_2) with either argument < 0, e.g. area_rhombus(-1, 2) or area_rhombus(1, -2).","commonSituations":"Diagonals measured as coordinate spans with signed direction; drafting/CAD exports with sign conventions; missing-data sentinels.","solutions":["Check both diagonals >= 0 before the call; correct the sign at the data source.","Convert signed spans to magnitudes with abs().","Validate imported geometry data before processing.","Catch ValueError for application-level error reporting."],"exampleFix":"// before\narea = area_rhombus(d1, d2)  # d1 = x2 - x1 may be negative\n\n# after\narea = area_rhombus(abs(x2 - x1), abs(y2 - y1))","handlingStrategy":"validation","validationCode":"if diagonal_1 < 0 or diagonal_2 < 0:\n    raise ValueError(f'rhombus diagonals must be >= 0: {diagonal_1}, {diagonal_2}')\narea = area_rhombus(diagonal_1, diagonal_2)","typeGuard":"def is_valid_diagonals(d1: float, d2: float) -> bool:\n    return d1 >= 0 and d2 >= 0","tryCatchPattern":"try:\n    area = area_rhombus(diagonal_1, diagonal_2)\nexcept ValueError as e:\n    raise ValueError(f'invalid rhombus input: {e}') from e","preventionTips":["Derive diagonals as abs(max - min) spans, not raw deltas.","Validate CAD exports before geometric processing."],"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"}