{"record":{"id":"8527867c505170de","repo":"TheAlgorithms/Python","slug":"coefficient-a-must-not-be-zero","errorCode":null,"errorMessage":"Coefficient 'a' must not be zero.","messagePattern":"Coefficient 'a' must not be zero\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/quadratic_equations_complex_numbers.py","lineNumber":20,"sourceCode":"\nfrom cmath import sqrt\n\n\ndef quadratic_roots(a: int, b: int, c: int) -> tuple[complex, complex]:\n    \"\"\"\n    Given the numerical coefficients a, b and c,\n    calculates the roots for any quadratic equation of the form ax^2 + bx + c\n\n    >>> quadratic_roots(a=1, b=3, c=-4)\n    (1.0, -4.0)\n    >>> quadratic_roots(5, 6, 1)\n    (-0.2, -1.0)\n    >>> quadratic_roots(1, -6, 25)\n    ((3+4j), (3-4j))\n    \"\"\"\n\n    if a == 0:\n        raise ValueError(\"Coefficient 'a' must not be zero.\")\n    delta = b * b - 4 * a * c\n\n    root_1 = (-b + sqrt(delta)) / (2 * a)\n    root_2 = (-b - sqrt(delta)) / (2 * a)\n\n    return (\n        root_1.real if not root_1.imag else root_1,\n        root_2.real if not root_2.imag else root_2,\n    )\n\n\ndef main():\n    solution1, solution2 = quadratic_roots(a=5, b=6, c=1)\n    print(f\"The solutions are: {solution1} and {solution2}\")\n\n\nif __name__ == \"__main__\":\n    main()","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/quadratic_equations_complex_numbers.py#L2-L38","documentation":"quadratic_roots() in maths/quadratic_equations_complex_numbers.py solves ax^2 + bx + c = 0 using cmath.sqrt so complex roots are supported. If a == 0 it raises ValueError(\"Coefficient 'a' must not be zero.\") because with a = 0 the equation is linear (bx + c = 0) and the formula (-b +/- sqrt(delta)) / (2a) would divide by zero. This is a domain guard, not a numerical-robustness check: b and c are never validated.","triggerScenarios":"Calling quadratic_roots(a=0, b=2, c=1), or building coefficients from user input / curve fitting where the x^2 term collapses to zero (e.g. fitting a parabola to collinear points).","commonSituations":"Generic equation solvers accepting arbitrary a, b, c triples; fitting code where the quadratic coefficient legitimately vanishes; forgetting the degenerate linear case entirely.","solutions":["Handle the linear case yourself before calling: if a == 0 and b != 0, root is -c/b; if a == b == 0, no/infinitely many solutions depending on c.","Validate a != 0 at the input boundary with a domain-specific message.","Catch ValueError if zero leading coefficients are an expected runtime input."],"exampleFix":"# before\nroots = quadratic_roots(a, b, c)  # ValueError when a == 0\n\n# after\nif a == 0:\n    roots = (-c / b,) if b else ()\nelse:\n    roots = quadratic_roots(a, b, c)","handlingStrategy":"validation","validationCode":"if a == 0:\n    root = -c / b if b else None  # linear or degenerate\nelse:\n    roots = quadratic_roots(a, b, c)","typeGuard":null,"tryCatchPattern":"try:\n    quadratic_roots(a, b, c)\nexcept ValueError as exc:\n    if \"must not be zero\" in str(exc):\n        # fall back to the linear solution bx + c = 0\n        roots = (-c / b,) if b else ()\n    else:\n        raise","preventionTips":["Always branch on a == 0 before using the quadratic formula.","In fitting code, detect collinear data that zeroes the quadratic term.","Only 'a' is validated; sanitize b and c yourself."],"tags":["python","value-error","quadratic","maths"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}